下载文件代码 [英] Code for Download files

查看:76
本文介绍了下载文件代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在我的网站中提供一个选项来上传多个文件,然后允许用户下载这些文件.
我已经完成了多个文件的上传,但是我还不清楚如何下载部分.
我首先想到了将超链接动态地添加到每个文件的标签中(因为我不确定用户将上传多少个文件).
但是随后它在浏览器中打开文件,并且不提供保存或打开文件的选项.
主要问题是用户可以提交任何类型的文件,例如ms doc或xls或文本文件等,因此内容类型不固定.

我不清楚我将如何精确地实现,我的意思是动态添加链接按钮或动态添加超链接.之后,我将如何下载文件?我不能做

 Response.WriteFile(Server.MapPath( @" 
解决方案

我的方法是创建一个ASPX文件来处理下载: br/>

 <%@    页面   语言  ="  C#"  AutoEventWireup    true"  CodeFile   ="  继承   wm5ftdl"   %> 

<%
    // 在给定文件名的情况下将下载文件发送到客户端.
    字符串 guid = Request.QueryString ["  span>];
    字符串 fileName = " ;
    字节 []数据=  字节 [ ] { 0  0  0  0 };
    字符串 strCon = System.Web.Configuration.WebConfigurationManager.ConnectionStrings [" ].ConnectionString;
    使用(System.Data.SqlClient.SqlConnection con =  System.Data.SqlClient.SqlConnection(strCon) )
        {
        con.Open();
        字符串 strcmd = "  +
                        "  +

                        " ;
        使用(System.Data.SqlClient.SqlCommand cmd =  System.Data.SqlClient.SqlCommand(strcmd,缺点))
            {
            cmd.Parameters.AddWithValue(" ,guid);
            使用(System.Data.SqlClient.SqlDataReader r = cmd.ExecuteReader())
                {
                如果(r.Read())
                    {
                    fileName =(字符串)r ["  span>];
                    数据=(字节 [])r [" ];
                    }
                }
            }
        }
    Response.Clear();
    Response.AddHeader(" " 无缓存,必须重新验证,post-check = 0,pre-check = 0");
    Response.AddHeader(" " 无缓存");
    Response.AddHeader(" " 文件下载");
    Response.AddHeader(" "  application/force-download");
    Response.AddHeader("  " );
    Response.AddHeader(" " 附件; filename =" + fileName);
    Response.BinaryWrite(data);
    //  Response.WriteFile("wm5ftdata/" + fileName); 
    Response.End();
%>  



CS文件非常空:

 公共 部分  class  wm5ftdl:System.Web.UI.Page
    {
    受保护的 无效 Page_Load(对象发​​件人,EventArgs e)
        {

        }
    } 



然后,网页上的下载链接转到以DB ID作为参数的ASPX文件:

 <   a     href   ="   target    _ blank" <  /a  >  


您尝试过吗?:

 Response.AddHeader(" "  + FileName); 


I have to give one option in my website to upload multiple files and then allowing users to download those files.
I have done uploading multiple files part, however I am not much clear how I will do downloading part.
I first thought to add hyperlinks dynamically to a label for each file (as I am not sure how many files user will upload).
But then it opens file in browser and does not give option to save or open file.
Main issue is user can submit any type of file like ms doc or xls or text files etc Hence content type is not fixed.

I am not clear on how exactly I will do it I mean adding link buttons dynamically or adding hyperlinks dynamically. And after that how will I download file? I am not able to do

Response.WriteFile(Server.MapPath(@"~/logo_large.gif"));

as content type is not clear.

解决方案

The way I do it is to create an ASPX file to handle the download:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="wm5ftdl.aspx.cs" Inherits="wm5ftdl" %>

<%
    // Send a download file to the client given the filename.
    string guid = Request.QueryString["file"];
    string fileName = "ERROR";
    byte[] data = new byte[] { 0, 0, 0, 0 };
    string strCon = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DownloadDatabase"].ConnectionString;
    using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strCon))
        {
        con.Open();
        string strcmd = "SELECT [iD] ,cn.[fileName],[description] ,[dataContent] ,[version] " +
                        "FROM dlContent cn " +

                        "WHERE cn.iD=@ID";
        using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(strcmd, con))
            {
            cmd.Parameters.AddWithValue("@ID", guid);
            using (System.Data.SqlClient.SqlDataReader r = cmd.ExecuteReader())
                {
                if (r.Read())
                    {
                    fileName = (string) r["filename"];
                    data = (byte[]) r["dataContent"];
                    }
                }
            }
        }
    Response.Clear();
    Response.AddHeader("Cache-Control", "no-cache, must-revalidate, post-check=0, pre-check=0");
    Response.AddHeader("Pragma", "no-cache");
    Response.AddHeader("Content-Description", "File Download");
    Response.AddHeader("Content-Type", "application/force-download");
    Response.AddHeader("Content-Transfer-Encoding", "binary\n");
    Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
    Response.BinaryWrite(data);
    //Response.WriteFile("wm5ftdata/" + fileName);
    Response.End();
%>



The CS file is pretty empty:

public partial class wm5ftdl: System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
        {

        }
    }



The download link on the webpage then goes to the ASPX file with the DB ID as a parameter:

<a href="wm5ftdl.aspx?file=1d84b1a2-4ee8-4b68-a10a-f1c74fd45835" target="_blank">MDG PM Sep2011.XLS</a>


Did you try this?:

Response.AddHeader("Content-Disposition", "attachment; filename=" +FileName);


这篇关于下载文件代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆