943,832 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Marked Solved
  • Views: 7424
  • ASP.NET RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 10th, 2007
0

Download File

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

Below is my upload method and it works.

ASP.NET Syntax (Toggle Plain Text)
  1. protected void uploadFile()
  2. {
  3. String projectID = (String)Session["projId"];
  4. //change the file name
  5. if (!FileUpload.FileName.Equals(""))
  6. {
  7. // Create a new folder in Docs folder (folder name = projectID)
  8. System.IO.Directory.CreateDirectory(Server.MapPath("Docs/") + projectID);
  9.  
  10. // Save the file into the new folder.
  11. FileUpload.PostedFile.SaveAs(Server.MapPath("Docs/" + projectID + "/" + FileUpload.FileName));
  12. lblFile.Text = FileUpload.FileName + " is uploaded!";
  13. lblFile.Visible = true;
  14.  
  15. }
  16. }//end of method


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

ASP.NET Syntax (Toggle Plain Text)
  1. protected void btnLink_OnClientClick(object sender, EventArgs e)
  2. {
  3. String projectID = (String)Session["projId"];
  4. String file1 = System.IO.Path.GetFileName(Server.MapPath("Docs/" + pid + "/"));
  5. }

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

Please help. Thanks!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
dotNetDummi is offline Offline
53 posts
since Nov 2007
Dec 10th, 2007
0

Re: Download File

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...
Reputation Points: 10
Solved Threads: 4
Newbie Poster
shaulf is offline Offline
18 posts
since Nov 2007
Dec 10th, 2007
0

Re: Download File

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
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
dotNetDummi is offline Offline
53 posts
since Nov 2007
Dec 10th, 2007
0

Re: Download File

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\
Reputation Points: 66
Solved Threads: 56
Posting Pro in Training
Fungus1487 is offline Offline
459 posts
since Apr 2007
Dec 10th, 2007
0

Re: Download File

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?
Last edited by dotNetDummi; Dec 10th, 2007 at 8:24 pm.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
dotNetDummi is offline Offline
53 posts
since Nov 2007
Dec 10th, 2007
0

Re: Download File

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
ASP.NET Syntax (Toggle Plain Text)
  1. FileInfo finfo = new FileInfo(Server.MapPath(MyFile));
  2. long FileInBytes = finfo.Length;

then use the 'FileInBytes' attribute to show the file length
Reputation Points: 66
Solved Threads: 56
Posting Pro in Training
Fungus1487 is offline Offline
459 posts
since Apr 2007
Dec 10th, 2007
0

Re: Download File

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
Reputation Points: 66
Solved Threads: 56
Posting Pro in Training
Fungus1487 is offline Offline
459 posts
since Apr 2007
Dec 10th, 2007
0

Re: Download File

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
dotNetDummi is offline Offline
53 posts
since Nov 2007
Dec 10th, 2007
0

Re: Download File

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
dotNetDummi is offline Offline
53 posts
since Nov 2007
Dec 10th, 2007
0

Re: Download File

I got it . I used another method found online.

ASP.NET Syntax (Toggle Plain Text)
  1. DirectoryInfo diSource = new DirectoryInfo(Server.MapPath("Docs/" + pid + "/EP/"));
  2. string ExactFilename = string.Empty;
  3.  
  4. foreach (FileInfo file in diSource.GetFiles(@"*.*"))
  5. {
  6. ExactFilename = file.FullName;
  7. }
  8. System.Diagnostics.Process.Start(ExactFilename);

Just that it opened the application immediately without the filedialog box.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
dotNetDummi is offline Offline
53 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Help Me ! (ASP.NET)
Next Thread in ASP.NET Forum Timeline: copy a project to another computer





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC