Solved

Browse button for saving file

Posted on 2011-09-02
11
187 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now