Solved

Browse button for saving file

Posted on 2011-09-02
11
191 Views
Last Modified: 2012-05-12
I have a page that is designed to create a pdf file but have no idea how to give the user the option to choose the location where to save the file. When I am creating the pdf, I supply the path so what I will need is to allow the user use something like windows explorer, choose the path and then pass that path to my createPDF method. Any idea how I can do this?

Sample code would help.

Thanks,
0
Comment
Question by:DowntownIT
  • 5
  • 5
11 Comments
 
LVL 12

Expert Comment

by:Wouter Boevink
ID: 36473955
The example below will force an 'save as' dialog, basicly this is done by setting the content-disposition to attachment.

Response.AppendHeader( "content-disposition", "attachment; filename=" + name );

private void DownloadFile( string fname, bool forceDownload )
{
  string path = MapPath( fname );
  string name = Path.GetFileName( path );
  string ext = Path.GetExtension( path );
  string type = "";
  // set known types based on file extension  
  if ( ext != null )
  {
    switch( ext.ToLower() )
    {
    case ".htm":
    case ".html":
      type = "text/HTML";
      break;
  
    case ".txt":
      type = "text/plain";
      break;
     
    case ".doc":
    case ".rtf":
      type = "Application/msword";
      break;
    }
  }
  if ( forceDownload )
  {
    Response.AppendHeader( "content-disposition",
        "attachment; filename=" + name );
  }
  if ( type != "" )   
    Response.ContentType = type;
  Response.WriteFile( path );
  Response.End();    
}

Open in new window

0
 
LVL 2

Author Comment

by:DowntownIT
ID: 36474149
Am I correct that this method requires that the file already exists? If so, I am looking for some way to capture a user chosen path that I then pass that path to the method that creates the pdf. So what I need to do is choose the path, capture path, then create the file.

I apologize if I am missing something,
0
 
LVL 12

Expert Comment

by:Wouter Boevink
ID: 36474192
Is the location to save the pdf at the server or the client computer?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:Wouter Boevink
ID: 36474213
The method described above could be used to create the pdf and show a dialog to save it on the client computer.
There is no dialog for saving something on the server. However you could create something, let me know if this is what you want.
0
 
LVL 2

Author Comment

by:DowntownIT
ID: 36474219
Ideally, it would be anywhere that the user chooses. More than likely a shared network folder.
0
 
LVL 12

Expert Comment

by:Wouter Boevink
ID: 36474255
And why can't this be accomplished by the save-as dialog? Do you want the user to save to any drive (including shared folders) attached to his computer? Or to save it on any drive (including shared folders) attached to the server?
0
 
LVL 2

Author Comment

by:DowntownIT
ID: 36474373
It looks like you need to supply this DownloadFile with an existing file. The file does not exist yet. My CreatePDF method does the saving. So I don't need something that will save a file but some way to browse Windows and capture the path as a string to pass to the CreatePDF method.
0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 36474627
Hello, you have to use the fileupload control.
0
 
LVL 2

Author Comment

by:DowntownIT
ID: 36474671
This is close except 2 things.

1. I am not uploading a file
2. I have to pick a file, all I want is the path to a folder or location. My current method will put the file in that location.
0
 
LVL 12

Accepted Solution

by:
Wouter Boevink earned 500 total points
ID: 36475223
You can use System.IO.DriveInfo to enumerate the drives on the server
http://msdn.microsoft.com/en-us/library/system.io.driveinfo.aspx

Then you can use System.IO.DirectoryInfo to enumerate the directories on that drive
http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.aspx

for network shares you can use NetShareEnum win32 api. http://www.pinvoke.net/default.aspx/netapi32/netshareenum.html
Or look at the following project which is .NET Wrapper over it. http://www.codeproject.com/KB/IP/networkshares.aspx

Then you can give to user an aspx page where he can choose the drive and directory on the server.

(Alternatively since its probably a network share where the file should be saved you can use the save-as dialog, in the example an existing file is used but you could supply an IO stream / File Stream / Memory stream it all depends on how the pdf is created)
0
 
LVL 2

Author Closing Comment

by:DowntownIT
ID: 36495333
Still playing around with this but looks like it is going to get me what I am looking for.

Thanks for the help!
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question