code下载文件 [英] Code to Download file

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

问题描述

我不得不放弃在我的网站一个选项上传多个文件,然后让用户下载这些文件。
我已经做了上传多个文件的一部分,不过我倒没清楚我该怎样做下载的一部分。
我首先想到的动态添加超链接到每个文件的标签(因为我不知道很多文件的用户将如何上传)。
但随后它会打开浏览器中的文件并没有给选项,保存或打开文件。
主要的问题是用户可以提交任何类型的文件,如MS文档或XLS文件或文本文件等内容。因此类型是不固定的。

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"));

作为内容类型不明确。
请帮我有关code下载所有类型的文件

as content type is not clear. Please help me regarding code for download all types of files

推荐答案

下载文件:

    public void DownLoad(string FName)
    {
        string path = FName;
        System.IO.FileInfo file = new System.IO.FileInfo(path);
        if (file.Exists)
        {
            Response.Clear();
            Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
            Response.AddHeader("Content-Length", file.Length.ToString());
            Response.ContentType = "application/octet-stream";
            Response.WriteFile(file.FullName);
            Response.End();

        }
        else
        {
            Response.Write("This file does not exist.");
        }

    }

但是这是Word的DOC / DOCX文件。在该行: Response.ContentType =应用程序/八位字节流; 你必须定义文件类型

显示超链接动态地写一个循环,并去了所有被用户和writh以下code上传的文件:

for displaying hyperlink dynamically write a loop and go over all the files which were uploaded by the user and writh the following code:

     yourdivId += "<a href='" + file.FullName + "' >" + file.Name + "</a></br>";

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

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