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

C# ASP-从streamwriter提示保存文件对话框,c#,asp.net,C#,Asp.net,目前,我的文本文件将进入桌面,在ASP中,如何为用户提示文件保存对话框?结果是来自streamreader的字符串,作为“结果”,如下所示: StreamWriter FileWriter = new StreamWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "file.txt")); FileWriter.Write(result);

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

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

                FileWriter.Write(result);

                FileWriter.Close();

如果我没有弄错的话,您首先有一个1层(桌面)应用程序,它使用C#将文件保存到用户的文件系统。现在,您正试图过渡到两层应用程序,您的C#代码在服务器层上运行,用户的浏览器代表另一层。在服务器上的C#中,您无法直接将文件写入浏览器用户的文件系统

也就是说,您需要做的是使用类似application/octet-stream的内容类型将文件写入HTTP响应流。您实际使用的是ASP还是ASP.NET?如果是后者,您应该可以通过各种方式访问响应流,但从响应对象开始(可从页面获得,也可通过HttpContext.Current.response获得)


我没有尝试代码,但可能您需要省略对
FileWriter.Close()
的调用,因为它将尝试处理流。如果不是,那么您应该使用
而不是
。如果问题太大,可以使用
write
方法直接写入流,或者使用
MemoryStream

我的一个应用程序中的一个例子

    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);
    }        
}

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

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

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();
        }

ASP?你现在所做的一切都不会用浏览器保存到用户的桌面上。请详细说明您要完成的任务好吗?基本上,我应该在哪里指定streamwriter来保存文件,以及如何使用“另存为”对话框提示用户保存文件?在链接或按钮是CLICKEDE之后,您可能会考虑设置<代码>响应。CordNoType =“Apple /OctTeFipe”;<代码>行到文件的实际mime类型(如果已知)。
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();
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
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();
        }