0

I need to set the save location in run time in mvc application . In windows appication we use the

System.Windows.Forms.SaveFileDialog();

But, what we use the web Application?

tereško
  • 58,060
  • 25
  • 98
  • 150
David Ferry
  • 283
  • 1
  • 4
  • 14

1 Answers1

2

It is not clear what you want to save. In a web application you could use the file input to upload files to the server:

<input type="file" name="file" />

For more information about uploading files in an ASP.NET MVC application you may take a look at the following post.

If on the other hand you want the user to be able to download some file from the server and prompted for the location where he wants to save this file you could return a File result from a controller action and specify the MIME type and filename:

public ActionResult Download()
{
    var file = Server.MapPath("~/App_Data/foo.txt");\
    return File(file, "text/plain", "foo.txt");
}

There are also other overloads of the File method that allow you to dynamically generate a file and pass it as a stream to the client. But the important part to understand in a web application when downloading a file from a server is the Content-Disposition header. It has 2 possible values: inline and attachment. For example with the above code the following header will be added to the response:

Content-Type: text/plain
Content-Disposition: attachment; filename=foo.txt

... contents of the file ...

When the browser receives this response from the server it will prompt the user with a Save As dialog allowing him to choose the location on his computer to store the downloaded file.


UPDATE:

Here's how you could achieve similar in a web application:

public ActionResult Download()
{
    var file1 = File.ReadAllLines(Firstfilpath);
    var file2 = File.ReadAllLines(2ndfilpath);
    var mergedFile = string.Concat(file1, file2);
    return File(mergedFile, "text/plain", "result.txt");
}
Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Darin, i get the two file in my server side. I need merge these two file and save the single file. i need to get that single file name for user manually location how its possible? – David Ferry Apr 11 '13 at 05:52
  • Only a single file can be streamed to the client. You could compress the 2 files in a single ZIP file and then return this ZIP file to the client. If by *merge* you mean something else then please explain. For example maybe those files are of some special type and can be merged. But first you have to provide more details about your specific scenario. – Darin Dimitrov Apr 11 '13 at 05:54
  • Now check the my post .This is for windows application. How to cahnge this web application? – David Ferry Apr 11 '13 at 05:58
  • @user279, I have updated my answer to provide an example of how to merge 2 text files and stream them to the client. See my UPDATE at the end. – Darin Dimitrov Apr 11 '13 at 06:05
  • Thanks darin. But export file name is only choose by the client side manually? – David Ferry Apr 11 '13 at 06:13
  • The server should provide a default filename and the client has the possibility to change it in the Save As dialog. There's no other way. – Darin Dimitrov Apr 11 '13 at 06:41