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!

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...

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

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\

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?

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

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

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.

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.

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.

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.

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.

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?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.