ASP - 从StreamWriter中提示保存文件对话框

3

目前我已经将文本文件保存到桌面,那么在ASP中如何为用户提示一个文件保存对话框?以下是从StreamReader中获得的字符串“result”:

StreamWriter FileWriter = new StreamWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "file.txt"));

                FileWriter.Write(result);

                FileWriter.Close();

1
ASP?你现在所做的事情不会将任何东西保存到使用浏览器的用户的桌面上。您能否进一步说明您试图实现什么? - Kirk Woll
基本上,我在哪里指定StreamWriter保存文件,以及如何在链接或按钮被点击后使用“另存为”对话框提示用户保存文件? - user406151
6个回答

5
String FileName = filename;
String FilePath = filepath;
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/plain";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.TransmitFile(FilePath + FileName);
response.Flush();
response.End();

2
Response.AppendHeader("Content-Disposition", "attachment");
StreamWriter FileWriter = new StreamWriter(Response.OutputStream);
FileWriter.Write(result);

我没有尝试过这段代码,但是也许你需要省略调用 FileWriter.Close() 的代码,因为它将尝试处理该流。如果不需要处理,则应使用 using 代替。如果太麻烦,请直接使用流的 Write 方法或使用 MemoryStream


2
我的一个应用程序提供了一个例子来解释这个概念。
    protected void Page_Load(object sender, EventArgs e)
{
    string tempFileName = Request["tempFileName"];  // the temp file to stream
    string attachFileName = Request["attachFileName"];  // the default name for the attached file

    System.IO.FileInfo file = new System.IO.FileInfo(Path.GetTempPath() + tempFileName);
    if (!file.Exists)
    {
        pFileNotFound.Visible = true;
        lblFileName.Text = tempFileName;
    }
    else
    {
        // clear the current output content from the buffer
        Response.Clear();

        // add the header that specifies the default filename for the 
        // Download/SaveAs dialog 
        Response.AddHeader("Content-Disposition", "attachment; filename=" + attachFileName);

        // add the header that specifies the file size, so that the browser
        // can show the download progress
        Response.AddHeader("Content-Length", file.Length.ToString());

        // specify that the response is a stream that cannot be read by the
        // client and must be downloaded
        Response.ContentType = "application/octet-stream";
        // send the file stream to the client
        Response.WriteFile(file.FullName);
    }        
}

如果已知文件的MIME类型,您可以考虑将Response.ContentType = "application/octet-stream";行设置为实际的MIME类型。 - Philip Smith

1
string path = Server.MapPath(your application path);
WebClient client = new WebClient();            
byte[] data = client.DownloadData(new Uri(path));
Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", String.Format("attachment; filename={0}", "aspnet.pdf"));
Response.OutputStream.Write(data, 0, data.Length);

try this code.. its helpful

1

不知道它是否仍然开放,但只是为了帮助其他人

只需使用此代码,它应该可以提示用户打开对话框以在系统上打开或保存文件....

byte[] bytesPDF = System.IO.File.ReadAllBytes(@"C:\sample.pdf");

        if (bytesPDF != null)
        {

            Response.AddHeader("content-disposition", "attachment;filename= DownloadSample.pdf");
            Response.ContentType = "application/octectstream";
            Response.BinaryWrite(bytesPDF);
            Response.End();
        }

0
如果我理解正确,您最初有一个一层(桌面)应用程序,使用C#将文件保存到用户的文件系统中。现在,您正在尝试过渡到一个2层应用程序,其中您的C#代码在服务器层运行,而用户的浏览器代表另一层。在服务器上,您无法直接使用C#将文件写入浏览器用户的文件系统。

话虽如此,您需要做的是使用内容类型为application/octet-stream将文件写入HTTP响应流中。您实际上是否正在使用ASP或ASP.NET?如果是后者,则可以通过各种方式访问响应流,但是从Response对象开始(可从页面获取,但也可通过HttpContext.Current.Response获取)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接