Solved

Browse button for saving file

Posted on 2011-09-02
11
190 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a‚Ķ
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall‚Ķ
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

829 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