ASP - a dialog box asking to save a file from a stream

I currently have a text file going to the desktop in ASP, how can I request a file save dialog for the user? The result is a string from streamreader as a "result" as follows:

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

                FileWriter.Write(result);

                FileWriter.Close();
+3
source share
6 answers
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();
+5
source
Response.AppendHeader("Content-Disposition", "attachment");
StreamWriter FileWriter = new StreamWriter(Response.OutputStream);
FileWriter.Write(result);

I have not tried the code, but maybe you need to omit the call FileWriter.Close()as it will try to delete the stream. If not, then you should use using. If this is too problematic, write directly to the stream using the method Writeor use MemoryStream.

+2
source

, .

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

, () , # . #, , , . # , .

As the saying goes, you need to write the file to the HTTP response stream with a content type, for example, application / octet-stream. Are you using ASP or ASP.NET? If the latter, you should have access to the response stream through various means, but start with the Response object (accessible from the page, but also accessible through HttpContext.Current.Response).

0
source

Source: https://habr.com/ru/post/1757225/


All Articles