html文件的写法,HTML上传文件写法

来源于:http://www.cnblogs.com/SkySoot/p/3525139.html

html 表单上传文件

一般处理程序由于没有 apsx 页面的整个模型和控件的创建周期,而比较有效率。这里写一个用 html 表单进行文件上传的示例。

1. 表单元素选用 控件。

2. form 表单需要设置 enctype="multipart/form-data" 属性,请求报文体中数据格式也由键值对更改为数据头和数具体,并有随机边界符分割。

3. 服务器端接收文件使用 Request.Files 属性。

4. 使用 HttpPostedFile 的 SaveAs 方法保存文件(需转换成网站物理路径)。

public void ProcessRequest(HttpContext context)

{

context.Response.ContentType = "text/html";

HttpServerUtility server = context.Server;

HttpRequest request = context.Request;

HttpResponse response = context.Response;

HttpPostedFile file = context.Request.Files[0];

if (file.ContentLength > 0)

{

string extName = Path.GetExtension(file.FileName);

string fileName = Guid.NewGuid().ToString();

string fullName = fileName + extName;

string imageFilter = ".jpg|.png|.gif|.ico";// 随便模拟几个图片类型

if (imageFilter.Contains(extName.ToLower()))

{

string phyFilePath = server.MapPath("~/Upload/Image/") + fullName;

file.SaveAs(phyFilePath);

response.Write("上传成功!文件名:" + fullName + "
");

response.Write(string.Format("%7B0%7D", fullName));

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值