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