.net 实现的上传下载,如果是图片就显示上传的图片

本文介绍了使用ASP.NET实现文件上传及下载的功能。通过HTML表单接收文件,并将其保存到服务器指定目录,同时展示了如何从服务器下载特定文件到客户端。

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

HTML:

        <div>
            <input id="fileUpload" type="file" runat="server" />
            <input id="btnFile" type="button" value="上传" runat="server" />
            <asp:Button ID="Button1" runat="server" Text="点击下载" OnClick="Button1_Click1" />
            <br />
            <br />

            <img alt="" src="<%=url%>" width="100" height="100" id="imgShow" />
        </div>

后台代码:

protected void Page_Load(object sender, EventArgs e)
        {
            btnFile.ServerClick += GetFile;
            
        }
        public string url = "";

        public void GetFile(object sender, EventArgs e) 
        {
            String[] Extensions = { ".gif", ".png", ".jpeg", ".jpg", ".bmp" };
            if (!Directory.Exists(Server.MapPath(@"/word/")))
            {
                Directory.CreateDirectory(Server.MapPath(@"/word/"));
            }

            fileUpload.PostedFile.SaveAs(Server.MapPath(@"/word/") + fileUpload.Value);

            if (Extensions.Contains("." + fileUpload.Value.Split('.')[1]))
            {
                url = @"/word/" + fileUpload.Value;
            }
        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
            string strFileName = "典型病例.rtf";
            Response.ContentType = "application/x-zip-compressed";
            //Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); 
            string filename = @"E:\个人练习\MvcDemo\WebTest\word20160620140544\" + strFileName;
            //为路径
            Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlPathEncode(strFileName));
            //Server.UrlPathEncode(); 解决文件名的乱码问题.
            Response.TransmitFile(filename); 
        }

 

转载于:https://www.cnblogs.com/llcdbk/p/5600531.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值