简体   繁体   English

如何使用FileStream启动另存为对话框

[英]How to Start Save-As Dialog with a FileStream

My project is a Sharepoint Web-App, and it will generate a text file for the user and offer it for the user to download. 我的项目是Sharepoint Web-App,它将为用户生成一个文本文件,并提供给用户下载。

The web-app generates a string value and should use FileStream to create a text file out of it. 该Web应用程序会生成一个字符串值,并应使用FileStream从中创建一个文本文件。 The user clicks a button and they should get a save-as dialogue. 用户单击一个按钮,他们应该获得另存为对话框。

Now, it is simple enough to use FileStream to write to the user's local drive, but how do you prompt them for the location and THEN save it there ? 现在,使用FileStream写入用户的本地驱动器已经足够简单了,但是如何提示他们输入位置,然后将其保存在那里

There are lots of questions about how to write to the local disk, and there are questions about how to open the save-dialogue in the browser. 关于如何写入本地磁盘有很多问题,还有关于如何在浏览器中打开保存对话的问题。 But I have not found any that answer the question of using the two methods together. 但是我没有找到任何答案可以同时使用这两种方法。

To write the file, we can use this : 要写文件, 我们可以这样

        using (var fs = new FileStream(@"C:\test.txt", FileMode.Create, FileAccess.ReadWrite)) {
            TextWriter tw = new StreamWriter(fs);
            tw.Write("Hello World");
            tw.Flush();
        }

and to open the save-as dialog, we can do this , which will tell the browser a file is ready for downloading which will open the browser's own save dialogue: 并打开“另存为”对话框, 我们可以这样做 ,这将告诉浏览器一个文件可供下载,这将打开浏览器自己的保存对话框:

            if (resp.IsClientConnected) {
                resp.Clear();
                resp.ClearHeaders();
                //Indicate the type of data being sent
                resp.ContentType = "text/plain";    //".htm", "html" = "text/html"
                resp.AppendHeader("Content-Disposition", "attachment; filename=\"" + Path.GetFileName(gentextfilename) + "\"");
                resp.AppendHeader("Content-Length", reader.BaseStream.Length.ToString());
                resp.TransmitFile(file_name); //does not buffer into memory, therefore scales better for large files and heavy usage
                resp.End();
            }

Now, is there a way to combine these? 现在,有没有办法将这些结合起来? I don't want to save the file to the server and then download it. 我不想将文件保存到服务器然后下载。 I want to save it directly to a save location specified by the user. 我想直接将其保存到用户指定的保存位置。 And to make all of this worse, it's a Sharepoint web-app, so it will run inside of Sharepoint, so I don't think you can use the folderBrowserDialog1 or SaveFileDialog method l ike you can in a desktop application . 更糟的是,这是一个Sharepoint Web应用程序,因此它将在Sharepoint内部运行,因此,我不认为您可以folderBrowserDialog1 在桌面应用程序中那样使用folderBrowserDialog1SaveFileDialog方法。

Summary of What Answer is Being Sought: 正在寻找答案的摘要:

How can I create a file (without creating one on the server) to save to download location of the user's choice? 如何创建一个文件(不在服务器上创建一个文件)以保存用户选择的下载位置?

No, there is no way to do that directly as you are used to in Windows desktop development. 不,没有像您在Windows桌面开发中那样直接执行此操作的方法。 This is the nature of the protocol used. 这就是所使用协议的性质。

You can't use the desktop file dialogs from within your ASP.NET application to ask the user where to save things. 您不能使用ASP.NET应用程序中的桌面文件对话框来询问用户将内容保存在何处。 You have to rely on the browser to do so. 您必须依靠浏览器来这样做。

The best option you have is what you already show: content disposition. 最好的选择是已经显示的内容内容。 Give the browser as much information as you can and let the user decide what to do with it. 给浏览器尽可能多的信息,让用户决定如何处理。

If you can use ASP.NET MVC, you can take advantage of the streamed responses, like FileStreamResult or using standard ASP.NET, using CopyTo . 如果可以使用ASP.NET MVC,则可以利用流式响应,例如FileStreamResult通过CopyTo使用标准ASP.NET。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM