954,566 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Download File

Hi experts,
I need a linkbutton/button to download the files which my clients have uploaded.

Below is my upload method and it works.

protected void uploadFile()
    {
        String projectID = (String)Session["projId"];
        //change the file name
        if (!FileUpload.FileName.Equals(""))
        {
            // Create a new folder in Docs folder (folder name = projectID)
            System.IO.Directory.CreateDirectory(Server.MapPath("Docs/") + projectID);

            // Save the file into the new folder.
            FileUpload.PostedFile.SaveAs(Server.MapPath("Docs/" + projectID + "/" + FileUpload.FileName));
            lblFile.Text = FileUpload.FileName + " is uploaded!";
            lblFile.Visible = true;

        }
    }//end of method

However, it's not working for downloading of file (in another asp)
May I know how can I do that?

protected void btnLink_OnClientClick(object sender, EventArgs e)
{
   String projectID = (String)Session["projId"];
   String file1 = System.IO.Path.GetFileName(Server.MapPath("Docs/" + pid + "/"));
}


The files format that are uploaded are not consistent. Usually it's excel, word or pdf format.

Please help. Thanks!

dotNetDummi
Junior Poster in Training
53 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

Not 100% sure what you are trying to do? Upload or Download a file. For download you need to replace the response stream with the file. This link is pretty good for downloading:

http://www.xefteri.com/articles/show.cfm?id=8

Use lots of try catch blocks...

shaulf
Newbie Poster
18 posts since Nov 2007
Reputation Points: 10
Solved Threads: 4
 

Hi. Thanks for your reply.
I'm trying to download files. By the way, I'm using C# so doesn't really quite understand the VB code.

I tried this .

protected void btnLink_OnClientClick(object sender, EventArgs e)
{
String pid = Convert.ToString(Session["pid"]);
String file = System.IO.Path.GetFileName(Server.MapPath("Docs/" + pid + "/EP/"));

Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file);
Response.End();
}

But it doesn't work .

Error msg : Access to the path 'C:\Documents and Settings\[machineName]\My Documents\[projectName]' is denied.

Is it because I upload at my documents?
What should I do? Please help. Thanks

dotNetDummi
Junior Poster in Training
53 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

set your permissions to allow access to my documents.
saying that i remember not being allowed access to a servers My Documents folder even with permissions set.
use another file location i.e.
c:\WEB APP FILES\

Fungus1487
Posting Pro in Training
459 posts since Apr 2007
Reputation Points: 66
Solved Threads: 56
 

Thanks for your reply.

Nope, I realised that variable ''file'' is empty.
But I thought that's the way to get the filename and extension in a directory path?

May I know how to do that?

dotNetDummi
Junior Poster in Training
53 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

well theres no file your just referencing the directory "Docs/##/EP/" ? try passing a filename through ?

also 'file.Length.ToString());' will not return the files size as its just a string variable you will need to do a

FileInfo finfo = new FileInfo(Server.MapPath(MyFile));
long FileInBytes = finfo.Length;


then use the 'FileInBytes' attribute to show the file length

Fungus1487
Posting Pro in Training
459 posts since Apr 2007
Reputation Points: 66
Solved Threads: 56
 

after re-reading you say you want them to download THE files.
the only way to do a batch download is either open a window for each file, create a zip file on the fly with all files inside, or have aprompt for them to download file sone at a time by selecting them

Fungus1487
Posting Pro in Training
459 posts since Apr 2007
Reputation Points: 66
Solved Threads: 56
 

Thanks for your reply.
You're fast.

Actually the thing is I wouldn't know what are the files that are being uploaded in the first place.

dotNetDummi
Junior Poster in Training
53 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

okay, I pass a filename already. It works.

Now I have to solve the problem.
To download the files which is "unknown" to my clients' users.

dotNetDummi
Junior Poster in Training
53 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

I got it . I used another method found online.

DirectoryInfo diSource = new DirectoryInfo(Server.MapPath("Docs/" + pid + "/EP/"));
        string ExactFilename = string.Empty;

        foreach (FileInfo file in diSource.GetFiles(@"*.*"))
        {
            ExactFilename = file.FullName;
        }      
        System.Diagnostics.Process.Start(ExactFilename);


Just that it opened the application immediately without the filedialog box.

dotNetDummi
Junior Poster in Training
53 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

if this is asp.net you wont get a file dialog on a web page.

best thing to do is as you have done print a list of files in the directory to the screen with a link for each one.

then have an iframe in the page. onclick of a link pass to the iframe the page you use to download and append the file path using GET method i.e.

download.aspx?filename=c:\mydocs\file1.jpg

then in the ONLOAD section of your web page codebehing you can use 'Request.Querystring('filename')' to attain this variable and perform the download.

this way you arent taking the user elsewhere when they download the file and it will look like its all integrated if the iframe is hidden.

in short an iframe is how most integrated upload download forms work on major websites.

Fungus1487
Posting Pro in Training
459 posts since Apr 2007
Reputation Points: 66
Solved Threads: 56
 

wow~~ That's cool.

So I get the file names and append to the link . aha! I need this method for my search function.
But I have to research on iFrame first.

I know it in Java but not too sure if it refers to internal frame. lol.

Thanks !!! I will try this.

dotNetDummi
Junior Poster in Training
53 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

erm, just to clarify, it will be similer like some music web sites right?

I can also do this in gridview, with different links to the uploaded file, am I right?

dotNetDummi
Junior Poster in Training
53 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 
internal frame


correct.it will be similer like some music web sites right?
correct.I can also do this in gridview, with different links to the uploaded file, am I right?
correct.


happy hunting.

Fungus1487
Posting Pro in Training
459 posts since Apr 2007
Reputation Points: 66
Solved Threads: 56
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You