ASP.NET 上传文件

使用一般处理程序上传文件

前台代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="myWeb.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server" method="post"  enctype="multipart/form-data" action="Handler1.ashx">
        <input type="file" name="img" value="" />
        <input type="submit" name="name" value="提交" /> 
    </form>
</body>
</html>

后台代码

 public void ProcessRequest(HttpContext context)
        { 
            #region 文件上传
            context.Response.ContentType = "text/plain";

            //获取文件
            HttpPostedFile file = context.Request.Files["img"];
//使用file.ContentLength来判断上传的附件是否为空
string fileType = Path.GetExtension(file.FileName); if (fileType == "cs") { context.Response.Write("请勿上传不符合规范的文件!"); } else { string path = "/upload/" + Guid.NewGuid().ToString() + file.FileName; file.SaveAs(context.Request.MapPath(path)); } //将图片显示到客户端 string imgPath = string.Format("<html><head></head><body><img src='{0}'/></body></html>", path); context.Response.Write(imgPath); #endregion 文件上传 }

 

 

使用服务器控件上传文件

前台代码

<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" /></div>
    <div>
        <asp:FileUpload ID="FileUpload2" runat="server" /></div>
    <div>
        <asp:FileUpload ID="FileUpload3" runat="server" /></div>
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" Style="margin-left: 90px" OnClick="Button1_Click" /></div>
    <asp:FileUpload ID="FileUpload4" runat="server" />
    </form>
    <div>
        <asp:FileUpload ID="FileUpload" runat="server" /></div>
</body>

后台代码

 protected void Button1_Click(object sender, EventArgs e)
        {
bool result = FileUpload.HasFile;//判断是否有附件
string filepath = "E:\\uploads"; #region 多个文件上传 HttpFileCollection uploadedFiles = Request.Files; for (int i = 0; i < uploadedFiles.Count; i++) { HttpPostedFile userPostedFile = uploadedFiles[i]; try { if (userPostedFile.ContentLength > 0) { userPostedFile.SaveAs(filepath + "\\" + Path.GetFileName(userPostedFile.FileName)); } } catch (Exception) { throw; } } //将文件的内容,放到Stream对象中 //Stream myStream; //myStream = FileUpload1.FileContent; //将文件的内容上传到byte数组中 //MemoryStream myStream; //myStream = (MemoryStream)FileUpload1.FileContent; //Byte[] myByteArray = new Byte[FileUpload1.PostedFile.ContentLength]; //myByteArray = myStream.ToArray(); #endregion #region 单个文件上传 //获取文件名 FileUpload.FileName; //获取文件长度 FileUpload.PostedFile.ContentLength //获取文件拓展名 Path.GetExtension(FileUpload.FileName) string pathSever = Server.MapPath("~") + "\\" + "Upload"; FileUpload.SaveAs(pathSever.Trim()); #endregion }
在上传的时候,若需要生成唯一文件名可以使用 Guid.NewGuid().ToString()

前台使用var fileName=$(this).val();
    fileName.substr(fileName.lastIndexOf('.'));
 来判断文件的类型
后台使用 Path.GetExtension方法判断文件的后缀名,以防破坏者上传入侵代码。

不过以上两点并不能完全解决问题,最终的解决方法是:

打开IIS,找到对应项目下的upload文件夹(通常项目中都会建一个类似的文件夹,用来专门存放用户上传的文件),选中该文件夹后,在屏幕左侧操作菜单下有一个 编辑功能权限,打开后取消脚本那个选项

posted @ 2017-08-01 10:34  水墨晨诗  阅读(195)  评论(0编辑  收藏  举报