Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0 .NET4
At a high-level this does not seem like rocket science, but it is proving to be a challenge. I am more familiar with c# windows form development. I am now creating a webpage and need to incorporate the equivalent of windows form SaveFileDialog functionality into the webpage. At the very least, I need to give the end user the ability to select their own file path and name for which data from a GridView will be saved. What are my options?
Posted 29-Sep-12 4:38am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I guess you want something like this:
FileStream sourceFile = new FileStream(Server.MapPath(@"FileName"), FileMode.Open); 
float FileSize; 
FileSize = sourceFile.Length; 
byte[] fileContent = new byte[(int)FileSize]; 
sourceFile.Read(fileContent, 0, (int)sourceFile.Length); 
sourceFile.Close(); 
Response.ClearContent(); 
Response.ClearHeaders(); 
Response.Buffer = true; 
Response.ContentType = "application/octet-stream"; 
Response.AddHeader("Content-Length", fileContent.Length.ToString()); 
Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName); 
Response.BinaryWrite(fileContent); 
Response.Flush(); 
Response.End(); 
 

Best regards
Espen Harlinn
  Permalink  
Comments
meganh40 at 29-Sep-12 11:06am
   
How do I ask the user what their mapped path is and allow them the ability to browse their directory structure?
Espen Harlinn at 29-Sep-12 11:15am
   
I assumed you wanted return a file from your web application, which executes on the server. If this is the case, then Server.MapPath is relative to your web application on the server. The above code enables you to send a file from your server side web application to the browser, which will open a "save as" dialog for the returned file.
meganh40 at 29-Sep-12 11:34am
   
Thanks so much. This worked perfectly!
Espen Harlinn at 29-Sep-12 11:34am
   
Brilliant :-D
ridoy at 29-Sep-12 13:43pm
   
+5
Espen Harlinn at 29-Sep-12 13:44pm
   
Thank you, ridoy :-D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 428
1 CHill60 310
2 BillWoodruff 183
3 DamithSL 163
4 OriginalGriff 163


Advertise | Privacy | Mobile
Web02 | 2.8.141022.1 | Last Updated 29 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100