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

时间:2010-07-29 20:29:36

标签: c# asp.net

目前我将文本文件转到桌面,在ASP中如何为用户提示文件保存对话框?结果是来自streamreader的字符串为“result”,如下所示:

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

                FileWriter.Write(result);

                FileWriter.Close();

6 个答案:

答案 0 :(得分: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();

答案 1 :(得分:2)

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

我没有尝试代码,但是你可能需要省略对FileWriter.Close()的调用,因为它会尝试处理流。如果没有,那么您应该使用using代替。如果问题太多,请使用Write方法直接写入流,或使用MemoryStream

答案 2 :(得分: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);
    }        
}

答案 3 :(得分: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

答案 4 :(得分: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();
        }

答案 5 :(得分:0)

如果我理解正确的话,你首先有一个1层(桌面)应用程序,它使用C#将文件保存到用户的文件系统。您现在正尝试使用在服务器层上运行的C#代码转换为2层应用程序,并且用户的浏览器代表另一层。您无法在服务器上的C#中直接将文件写入浏览器用户的文件系统。

话虽这么说,您需要做的是将文件写入HTTP响应流,其内容类型类似于application / octet-stream。你真的使用ASP或ASP.NET吗?如果是后者,您应该通过各种方式访问​​响应流,但是从Response对象开始(可从页面获得,但也可以通过HttpContext.Current.Response获得)。