views:

322

answers:

3

Hello,

I have an ASP.NET web application and I need to put data from the web page to the output text file. I would like to give user an ability to select the folder where the file will be saved. For example, when the user clicks on the 'Browse' button the select folder dialog should appear.

Is it possible to implement such thing in the asp.net web application?

Thanks,

Sergey

A: 

Using <input type="file"> the user can only browse the files on his computer. There's no way for him to see folders on the server unless you give him a list or treeview structure so that he can choose from. Here's an example of building such a treeview.

Darin Dimitrov
+1  A: 

EDIT:

Looking at your comment, I think you mean push to the response stream instead?

 protected void lnbDownloadFile_Click(object sender, EventArgs e)
 {
  String YourFilepath;
  System.IO.FileInfo file = 
  new System.IO.FileInfo(YourFilepath); // full file path on disk
  Response.ClearContent(); // neded to clear previous (if any) written content
  Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
  Response.AddHeader("Content-Length", file.Length.ToString());
  Response.ContentType = "text/plain";
  Response.TransmitFile(file.FullName);
  Response.End();
 }

This should display a dialog box in the browser allowing the user to select where to save the file.

You want to use the FileUpload control

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.aspx

protected void UploadButton_Click(object sender, EventArgs e)
  {
    // Specify the path on the server to
    // save the uploaded file to.
    String savePath = @"c:\temp\uploads\";

    // Before attempting to perform operations
    // on the file, verify that the FileUpload 
    // control contains a file.
    if (FileUpload1.HasFile)
    {
      // Get the name of the file to upload.
      String fileName = FileUpload1.FileName;

      // Append the name of the file to upload to the path.
      savePath += fileName;


      // Call the SaveAs method to save the 
      // uploaded file to the specified path.
      // This example does not perform all
      // the necessary error checking.               
      // If a file with the same name
      // already exists in the specified path,  
      // the uploaded file overwrites it.
      FileUpload1.SaveAs(savePath);

      // Notify the user of the name of the file
      // was saved under.
      UploadStatusLabel.Text = "Your file was saved as " + fileName;
    }
    else
    {      
      // Notify the user that a file was not uploaded.
      UploadStatusLabel.Text = "You did not specify a file to upload.";
    }

  }
hearn
not sure that i need FileUpload control. This control gives me ability to upload files to server. what i need is to give a client an ability to select local folder on his machine and save the file to this folder. i don't need to upload file to server. i need to save file in the specified folder of the local machine
Sergey
edited answer to show how to push file to the response stream.
hearn
thanks for the answer! that is what i needed. Could i ask you one more thing? Is it possible to show the 'select file' dialog box without the first window 'Open or Save file'?
Sergey
no probs!What you see in terms of open/save is dependent on the browser implementation, but for security reasons your application cannot modify this dialog.
hearn
+1  A: 

Such download dialog is browser specific.

Have a look at generic handlers with Response.Write or even better write a Http Handler for such purpose.

citronas