asp.net 文件上传代码
2015-05-26 23:13
621 查看
一般处理程序
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI.WebControls; using System.IO; namespace web { /// <summary> /// iputDecoment 的摘要说明 /// </summary> public class iputDecoment : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/html"; String Name = context.Request["docName"]; String Nsg = context.Request["DocMsg"]; HttpPostedFile docWords = context.Request.Files["Decoment"]; string filename = DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + Path.GetExtension(docWords.FileName);//有bug的,一毫秒内多个人上传多个文件 string path = context.Server.MapPath("~/Doc/" + filename); docWords.SaveAs(path); context.Response.Write("Hello World"); //SqlHelper.ExecuteNonQuery("Insert into T_Products(Name,CategoryId,ImagePath,Msg) values(@Name,@CategoryId,@ImagePath,@Msg)", new SqlParameter("@Name", name), new SqlParameter("@CategoryId", categoryId), new SqlParameter("@ImagePath", "/uploadfile/" + filename), new SqlParameter("@Msg", msg)); //context.Response.Redirect("ProductList.ashx"); } public bool IsReusable { get { return false; } } } }
HTML文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>文件上传</title> </head> <body> <form action="../iputDecoment.ashx" method="post" enctype="multipart/form-data"> <h1>文件上传</h1> <br /> 文件名:<input type="text" id="DocName" name="docName" "/><br /> 上传文件:<input type="file" name="Decoment" /><br /> 文件描述:<br /> <textarea name="DocMsg" cols="20" rows="5"></textarea> <input type="submit" value="提交" name="Save" /> <input type="reset" value="重置" name="Save" /> </form> </body> </html>需注意的是必须记得加入:
enctype="multipart/form-data"
<span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.2000007629395px; color: red;">enctype="multipart/form-data"是上传二进制数据; form里面的input的值以2进制的方式传过去。 </span><span style="font-family: Helvetica, Tahoma, Arial, sans-serif; font-size: 14px; line-height: 25.2000007629395px;">form里面的input的值以2进制的方式传过去,所以request就得不到值了。</span>
相关文章推荐
- asp.net 多文件上传,兼容IE6/7/8,提供完整代码下载
- asp.net(C#)上传下载及文件管理代码
- 【原创】Ext 上传文件,前后台实现, Asp.net 代码
- [代码示例]用Fine Uploader+ASP.NET MVC实现ajax文件上传
- [Fine Uploader] 用Fine Uploader+ASP.NET MVC实现ajax文件上传[代码示例]
- 如何限制asp.net中上传文件的大小的代码
- ASP.NET上传文件实例代码
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- ASP.NET(C#)实现一次性动态上传多张图片的代码(多个文件)
- asp.net实现ftp上传代码(解决大文件上传问题)
- ASP.NET上传和下载文件的代码
- asp.net 多文件上传,兼容IE6/7/8,提供完整代码下载
- ASP.NET 多文件上传代码
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- [导入]Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- 实现ASP.NET多文件上传程序代码
- 20160113006 asp.net实现ftp上传代码(解决大文件上传问题)
- Asp.net中上传大的文件(Vb.net代码)
- ASP.NET杜绝文件上传漏洞的代码