FileUpload文件上传

本文介绍了如何在web应用中实现文件上传和下载。在前端,使用控件进行文件选择;在后台.cs文件中,通过代码处理文件上传,并声明全局变量存储上传路径。同时,提供了文件下载的相关示例链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在web页面前端控件:

 

  <form id="form1" runat="server">
    <div style=" height:130px;">
       <asp:FileUpload  ID="file" runat="server"/><asp:Button runat="server" Text="上传" OnClick="FileUp"/>
    </div>
    <div style=" height:300px;">
    <asp:LinkButton runat="server"  OnClick="HrefClik">下载</asp:LinkButton>
    </div>
    </form>


在.cs后台文件中,

文件的上传为:

声明一个全局变量,记录上传的路径:

 private static  string docPath = "";

  //附件
            System.Web.HttpFileCollection contentFile = System.Web.HttpContext.Current.Request.Files;
            string fileName = string.Empty;
            string newFileName = string.Empty;
            string contentPath = string.Empty;
            for (int iFile = 0; iFile < contentFile.Count; iFile++)
            {
                HttpPostedFile postedFile = contentFile[iFile];
                fileName = System.IO.Path.GetFileName(postedFile.FileName);

                if (fileName != "")
                {
                    if (postedFile.ContentLength > 20 * 1024 * 1024)//限定文件大小
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('上传文件大小不得大于20M');", true);
                        return;
                    }
                    //上传文件类型判断
                    String filetype = contentFile[iFile].FileName.Substring(contentFile[iFile].FileName.LastIndexOf(".") + 1);
                    if (!(filetype.Equals("doc") || filetype.Equals("docx") || filetype.Equals("JPG") || filetype.Equals("jpg")))
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('只能上传doc,docx格式的文件或者jpg图片');", true);
                        return;
                    }
                    fileName = (contentFile[iFile].FileName.LastIndexOf("\\") >= 0 ? contentFile[iFile].FileName.Substring(contentFile[iFile].FileName.LastIndexOf("\\") + 1) : contentFile[iFile].FileName);
                    newFileName = Guid.NewGuid().ToString() + "." + filetype;

                    contentPath = Server.MapPath("/SendMsgUpFile/" + newFileName);//文件在服务器中存储的路径
                    docPath = "/SendMsgUpFile/" + newFileName;
                    if (System.IO.File.Exists(contentPath))
                    {
                        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('服务器上已经存在同名文件,请使用其他文件名!');", true);
                        return;
                    }
                    contentFile[iFile].SaveAs(contentPath);
                }
            }
            Response.Write("上传成功");


 

文件的下载:

  //下载 
            string filename = "";
            string filepath = Server.UrlDecode(docPath);
            filename = Server.MapPath(filepath);
            Response.ContentType = "application/x-zip-compressed";
            filename = filename.Substring(filename.LastIndexOf("\\") + 1);
            Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename));
            Response.TransmitFile(filepath);


相关Demo:http://download.youkuaiyun.com/detail/hugaozhuang/5630815

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值