?
Solved

Browse button for saving file

Posted on 2011-09-02
11
Medium Priority
?
193 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

762 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