Solved

Browse button for saving file

Posted on 2011-09-02
11
189 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to reference objects of the calling class without passing them as parameters 29 176
Error in JQuery 5 52
mail.To.Add 5 26
Linq asp.net mvc 13 9
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

773 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