asp.net中通过input file上传文件

来源:互联网 发布:python编程培训 编辑:程序博客网 时间:2024/04/30 19:18

前台:

<input id="htmlFile" type="file" runat="server" />

 

后台:

if (Request.Files.Count > 0)
{
        HttpPostedFile file = Request.Files[0];

        int index = file.FileName.LastIndexOf("\\");
        int length = file.FileName.Length - index - 1;

        string file_name = file.FileName.Substring(index + 1, length);
        file.SaveAs(this.Server.MapPath("~/Files/" + file_name));

        Response.Write("<script type='text/javascript'>alert('Upload Success!');</script>");
}

 

此外还需注意,在asp.net的中使用<input type="file" />控件上传文件时是有大小限制的,可通过修改web.config文件中的参数来进行设置:

在<system.web>节点下增加<httpRuntime maxRequestLength="102400"/>一项(该属性是以字节为单位)

0 0