asp.net 上传
开始以为会很麻烦,需要什么读二进制流写二进制流的,结果自己试了一下,还真简单。。直接一个saveAs就行了。。。
下面是HTML代码:
代码
<form id="form2" action="Handle/Upload.ashx" method="post" enctype="multipart/form-data" >
<input type="file" id="imgFileName" name="imgName" style="width:220px;" />
<input type="submit" value="上传" />
</form>
<input type="file" id="imgFileName" name="imgName" style="width:220px;" />
<input type="submit" value="上传" />
</form>
在这里我是用了一般处理程序来进行文件的上传,代码如下:
代码
<%@ WebHandler Language="C#" Class="Upload" %>
using System;
using System.Web;
public class Upload : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
HttpPostedFile imgFile = context.Request.Files["imgName"];
String fileName=imgFile.FileName;
fileName = fileName.Substring(fileName.LastIndexOf("\\")+1);
String savePath = context.Server.MapPath("../Upload/") + fileName;
imgFile.SaveAs(savePath);
context.Response.Write("<br>保存成功!文件名:" + fileName);
}
public bool IsReusable {
get {
return false;
}
}
}
using System;
using System.Web;
public class Upload : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
HttpPostedFile imgFile = context.Request.Files["imgName"];
String fileName=imgFile.FileName;
fileName = fileName.Substring(fileName.LastIndexOf("\\")+1);
String savePath = context.Server.MapPath("../Upload/") + fileName;
imgFile.SaveAs(savePath);
context.Response.Write("<br>保存成功!文件名:" + fileName);
}
public bool IsReusable {
get {
return false;
}
}
}