文件无刷新上传 谁能给俺个例子,最好是基于JQUERY希望能控制文件名的找了好久没找到... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.cnblogs.com/caodaiming/archive/2009/05/13/1456273.html http://download.csdn.net/source/1378654 不好意思,复制错了网站http://www.111cn.net/wy/139/c39f19a319636978d2364cabc26cffe4.htm 用swfupload 可以实现无刷新 多文件上传 引用到的js ----ajaxfileupload.js<script type="text/javascript"> function ajaxFileUpload(filename) { $.ajaxFileUpload ( { url: 'imageFile.ashx?param=uploadfile&filename='+filename, secureuri: false, fileElementId: filename, dataType: 'json', success: function(data, status) { if (typeof (data.error) != 'undefined') { if (data.error != '') { alert(data.error); } else { alert(data.msg); insertimgBox(filename,data.filepath); } } } , error: function(data, status, e) { alert(e); } } ); return false; }</script>页面 <input type="file" runat="server" id="filepost_PeopleHealth"/><button id="button7" onclick="return ajaxFileUpload('filepost_PeopleHealth');">上传</button>imageFile.ashx文件using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web;using System.IO;using System.Drawing;namespace AjaxLibrary{ public class imageFile:IHttpHandler { bool IHttpHandler.IsReusable { get { return false; } } void IHttpHandler.ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html";//设置输出的类型*** string param=context.Request.QueryString["param"];//类型 string filenames = context.Request.QueryString["filename"]; string error = ""; string msg = ""; string filePath = ""; switch (param) { case "uploadfile": bool boolen = false; HttpPostedFile postFile = context.Request.Files[filenames];//httpPostFile if (postFile.ContentLength == 0) { error = "无效的文件"; } else { string mapPathDir="~/upload_files/"; if(System.IO.Directory.Exists(context.Server.MapPath(mapPathDir))!=true) { System.IO.Directory.CreateDirectory(context.Server.MapPath(mapPathDir)); } UploadFile(context, postFile, mapPathDir,boolen,out error,out msg,out filePath); } string result = "{error:'"+error+"',msg:'"+msg+"',filepath:'"+filePath+"'}"; context.Response.Output.Write(result); break; case "delfile": string RequestFilePath = context.Request.QueryString["filepath"];//获取要删除的图片的路径() 如:upload_files/XXXXYYY.gif DelImgFile(context, RequestFilePath, out error, out msg); string delresult = "{ error:'" + error + "', msg:'" + msg + "'}"; context.Response.Output.Write(delresult); break; } } void UploadFile(HttpContext context,HttpPostedFile httppostFile,string paths,bool boolBenl,out string err,out string msg,out string filepath) { string fileName =Path.GetExtension(httppostFile.FileName).ToLower(); string firstName = DateTime.Now.ToString("yyyyMMddHHmmss"); string ThumbName = "Thumb_" + DateTime.Now.ToString("yyyyMMddHHmmss"); string newNamePath =paths+firstName + fileName; string ThumbNamePath =context.Server.MapPath(paths + ThumbName + fileName); string mapPathStr = context.Server.MapPath(newNamePath); httppostFile.SaveAs(mapPathStr); if (boolBenl == true) { CoverDoubleImage(httppostFile,202,200,ThumbNamePath); } err = ""; msg = "上传成功"; filepath = firstName + fileName; } //删除目录文件 void DelImgFile(HttpContext context, string imgName,out string error,out string msg) { if (imgName.Length != 0 && imgName.IndexOf('/')>-1) { string paths = context.Server.MapPath(imgName); string Thumbpaths = context.Server.MapPath(imgName.Split('/')[0] + "/Thumb_" + imgName.Split('/')[1]); try { if (System.IO.File.Exists(paths) == true) File.Delete(paths); if (System.IO.File.Exists(Thumbpaths) == true) File.Delete(Thumbpaths); } catch (Exception ee) { error = ee.Message; } error = ""; msg = "删除成功"; } else { error = ""; msg = "无效的请求"; } } #region 上传图片并自动生成缩略图JPG /// <summary> /// 上传图片并自动生成缩略图JPG /// </summary> /// <param name="FileUpload1">FileUpload控件引用</param> /// <param name="tWidth">缩略图宽度</param> /// <param name="tHeight">缩略图高度</param> /// <param name="savePath">缩略图路径</param> public static void CoverDoubleImage(HttpPostedFile FileUpload1, int tWidth, int tHeight, string savePath) { //检查上传文件的格式是否有效 if (FileUpload1.ContentType.ToLower().IndexOf("image") < 0) { // Use.JS.Alert("图片格式错误"); return; } //生成原图 System.IO.Stream oStream = FileUpload1.InputStream; System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream); int oWidth = oImage.Width; //原图宽度 int oHeight = oImage.Height; //原图高度 //生成缩略原图 Bitmap tImage = new Bitmap(tWidth, tHeight); Graphics g = Graphics.FromImage(tImage); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度 g.Clear(Color.Transparent); //清空画布并以透明背景色填充 g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel); try { //以JPG格式保存图片 //oImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg); //保存原图 tImage.Save(savePath,System.Drawing.Imaging.ImageFormat.Jpeg); //保存缩略图 } catch (Exception ex) { throw ex; } finally { //释放资源 oImage.Dispose(); g.Dispose(); tImage.Dispose(); } } #endregion }} 不好意思 俺试了 不行....不知道哪里出问题了,根本就没调用哪个ASHX文件 忘记说了,,还要注册<httpHandlers> <remove verb="*" path="*.asmx"/> <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/> <add path="*/coolite.axd" verb="*" type="Coolite.Ext.Web.ResourceManager" validate="false"/> <add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="false"/> <add path="imageFile.ashx" verb="*" type="AjaxLibrary.imageFile,AjaxLibrary"/> </httpHandlers>还有那个引用的那个js要到网上下个, 未能加载文件或程序集“AjaxLibrary”或它的某一个依赖项。系统找不到指定的文件 首先:那个js网上自己下其次:AjaxLibrary是我自己的程序集名称。你根据你项目的情况来定啊,再其次:注册我已经和你说了,代码该帖的都帖出了,最后。再不懂。我只能无语。给你的代码你自己要研究。不要拿着就用,要明白什么意思 我已传上去了去下载http://download.csdn.net/source/2503395 http://www.codefans.net/soft/4029.shtml这里不需要积分 我擦 十分还说不用....在请教一个问题,用这个控件的input file 我用click模拟点击事件,文件是选中了但是却不进提交事件,您知道咋么回事么? 您的代码是有问题滴,不过俺最后是用ajaxfileupload.js实现的,分给你了! JS代码混淆保护问题 求M选N算法 非常急 求高手 急、、、求救! 救命啊。您试图打开的项目是web项目。需要通过指定其的URL路径来打开它 为什么我的样式表不能应用到表格中? 急用js时间控件(要有精确到秒的)在线等 一大早问大家一个SQL的问题! 生成的静态页面,如新闻发布,在浏览时,怎样用<script src="xx.asp"></script>才能做到点击次数加1? 创建文件问题,急!给高分!! C# Microsoft.Practices.Unity特性注入问题 也问个asp.net的奇怪问题,100分,在线等 求教用户权限划分
引用到的js ----ajaxfileupload.js
<script type="text/javascript">
function ajaxFileUpload(filename) {
$.ajaxFileUpload
(
{
url: 'imageFile.ashx?param=uploadfile&filename='+filename,
secureuri: false,
fileElementId: filename,
dataType: 'json',
success: function(data, status) {
if (typeof (data.error) != 'undefined') {
if (data.error != '') {
alert(data.error);
} else {
alert(data.msg);
insertimgBox(filename,data.filepath);
} }
}
,
error: function(data, status, e) {
alert(e);
}
}
);
return false;
}
</script>页面
<input type="file" runat="server" id="filepost_PeopleHealth"/><button id="button7" onclick="return ajaxFileUpload('filepost_PeopleHealth');">上传</button>imageFile.ashx文件using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;
using System.Drawing;namespace AjaxLibrary
{
public class imageFile:IHttpHandler
{
bool IHttpHandler.IsReusable
{
get { return false; }
}
void IHttpHandler.ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";//设置输出的类型***
string param=context.Request.QueryString["param"];//类型 string filenames = context.Request.QueryString["filename"];
string error = "";
string msg = "";
string filePath = "";
switch (param)
{
case "uploadfile":
bool boolen = false;
HttpPostedFile postFile = context.Request.Files[filenames];//httpPostFile
if (postFile.ContentLength == 0)
{
error = "无效的文件";
}
else
{
string mapPathDir="~/upload_files/";
if(System.IO.Directory.Exists(context.Server.MapPath(mapPathDir))!=true)
{
System.IO.Directory.CreateDirectory(context.Server.MapPath(mapPathDir));
}
UploadFile(context, postFile, mapPathDir,boolen,out error,out msg,out filePath);
}
string result = "{error:'"+error+"',msg:'"+msg+"',filepath:'"+filePath+"'}"; context.Response.Output.Write(result);
break;
case "delfile": string RequestFilePath = context.Request.QueryString["filepath"];//获取要删除的图片的路径() 如:upload_files/XXXXYYY.gif DelImgFile(context, RequestFilePath, out error, out msg);
string delresult = "{ error:'" + error + "', msg:'" + msg + "'}"; context.Response.Output.Write(delresult); break;
}
} void UploadFile(HttpContext context,HttpPostedFile httppostFile,string paths,bool boolBenl,out string err,out string msg,out string filepath)
{
string fileName =Path.GetExtension(httppostFile.FileName).ToLower();
string firstName = DateTime.Now.ToString("yyyyMMddHHmmss");
string ThumbName = "Thumb_" + DateTime.Now.ToString("yyyyMMddHHmmss");
string newNamePath =paths+firstName + fileName;
string ThumbNamePath =context.Server.MapPath(paths + ThumbName + fileName);
string mapPathStr = context.Server.MapPath(newNamePath);
httppostFile.SaveAs(mapPathStr);
if (boolBenl == true)
{
CoverDoubleImage(httppostFile,202,200,ThumbNamePath);
}
err = "";
msg = "上传成功";
filepath = firstName + fileName;
} //删除目录文件
void DelImgFile(HttpContext context, string imgName,out string error,out string msg)
{
if (imgName.Length != 0 && imgName.IndexOf('/')>-1)
{
string paths = context.Server.MapPath(imgName); string Thumbpaths = context.Server.MapPath(imgName.Split('/')[0] + "/Thumb_" + imgName.Split('/')[1]);
try
{
if (System.IO.File.Exists(paths) == true)
File.Delete(paths);
if (System.IO.File.Exists(Thumbpaths) == true)
File.Delete(Thumbpaths);
}
catch (Exception ee) { error = ee.Message; }
error = "";
msg = "删除成功";
}
else
{
error = "";
msg = "无效的请求";
}
} #region 上传图片并自动生成缩略图JPG
/// <summary>
/// 上传图片并自动生成缩略图JPG
/// </summary>
/// <param name="FileUpload1">FileUpload控件引用</param>
/// <param name="tWidth">缩略图宽度</param>
/// <param name="tHeight">缩略图高度</param>
/// <param name="savePath">缩略图路径</param>
public static void CoverDoubleImage(HttpPostedFile FileUpload1, int tWidth, int tHeight, string savePath)
{
//检查上传文件的格式是否有效
if (FileUpload1.ContentType.ToLower().IndexOf("image") < 0)
{
// Use.JS.Alert("图片格式错误");
return;
} //生成原图
System.IO.Stream oStream = FileUpload1.InputStream;
System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream); int oWidth = oImage.Width; //原图宽度
int oHeight = oImage.Height; //原图高度 //生成缩略原图
Bitmap tImage = new Bitmap(tWidth, tHeight);
Graphics g = Graphics.FromImage(tImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度
g.Clear(Color.Transparent); //清空画布并以透明背景色填充
g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel);
try
{
//以JPG格式保存图片
//oImage.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg); //保存原图
tImage.Save(savePath,System.Drawing.Imaging.ImageFormat.Jpeg); //保存缩略图
}
catch (Exception ex)
{
throw ex;
}
finally
{
//释放资源
oImage.Dispose();
g.Dispose();
tImage.Dispose();
}
}
#endregion }
}
<httpHandlers>
<remove verb="*" path="*.asmx"/>
<add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" validate="false"/>
<add path="*/coolite.axd" verb="*" type="Coolite.Ext.Web.ResourceManager" validate="false"/>
<add path="Reserved.ReportViewerWebControl.axd" verb="*" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="false"/> <add path="imageFile.ashx" verb="*" type="AjaxLibrary.imageFile,AjaxLibrary"/> </httpHandlers>还有那个引用的那个js要到网上下个,
其次:AjaxLibrary是我自己的程序集名称。你根据你项目的情况来定啊,
再其次:注册我已经和你说了,代码该帖的都帖出了,
最后。再不懂。我只能无语。
给你的代码你自己要研究。不要拿着就用,要明白什么意思
http://download.csdn.net/source/2503395
在请教一个问题,用这个控件的input file 我用click模拟点击事件,文件是选中了但是却不进提交事件,您知道咋么回事么?