user1181942
user1181942

Reputation: 1607

Code to Download file

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. Please help me regarding code for download all types of files

Upvotes: 2

Views: 21298

Answers (5)

Alberto De Caro
Alberto De Caro

Reputation: 5213

Actually you can follow different ways.

  1. Guide the user during upload and make him specify the file type before starting upload. So the user has to select the content type from a selection. You need to save the correlation between the file and its content type.

  2. Create a map between file extensions and mime-types (See here for example). You need to obtain the file extension and save the correlation between that file and its content type.

  3. Try to identify the content type automatically. There is a Windows API that let you identify the Mime Type of a file. And here is some code.

After that, you can use st mnmn solution.

Upvotes: 1

Tami
Tami

Reputation: 586

Hope this will help u.()

 Response.Clear();
 Response.ContentType = YourFile.ToString();
 Response.AddHeader("Content-Disposition", "attachment;filename=" + YourFileName);
 Response.OutputStream.Write(YourFile, 0, YourFile.Length);
 Response.Flush();
 Response.End();

I have tried this to download img,txtfile,zip file,doc it works fine..
but little problem user will face that in my case when i opened a word(doc) file its ask 
me to openwith..? when i select MsWord it opened it correctly.

For showing these file you can make a grid view to show all file with a download link...

Upvotes: 1

Software Engineer
Software Engineer

Reputation: 3956

You'll have to set few response headers before server sends the file to client. See How to Download a file with a hyperlink

Upvotes: 0

st mnmn
st mnmn

Reputation: 3669

for download the file:

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

    }

but this is for word doc/docx files. in the line: Response.ContentType = "application/octet-stream"; you have to define the type of the file.

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

Upvotes: 6

Niranjan Singh
Niranjan Singh

Reputation: 18290

You can do it like this:

try
        {
            string strURL=txtFileName.Text;
            WebClient req=new WebClient();
            HttpResponse response = HttpContext.Current.Response;
            response.Clear();
            response.ClearContent();
            response.ClearHeaders();
            response.Buffer= true;
            response.AddHeader("Content-Disposition","attachment;filename=\"" + Server.MapPath(strURL) + "\"");
            byte[] data=req.DownloadData(Server.MapPath(strURL));
            response.BinaryWrite(data);
            response.End();
        }
        catch(Exception ex)
        {
     }

Upvotes: 0

Related Questions