Response.Flush() method is not working - asp.net

Response.Flush method is not working. I am getting the data at pdfContent but it is not opening the PDF documnet. Below is the code. Please help.
public ActionResult PdfClick(int requestid)
{
BusinessRequestController bsnrqstcntrlr = new BusinessRequestController();
try
{
int DocId = (new BusinessRequestBR()).GetBaseLineDocumentsForSearch(requestid);
byte[] PdfContent = (new BusinessRequestHelper()).GetBaseLineDonload(DocId);
Response.Buffer = true;
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=" + "BaseLine_Doc" + "_" + DocId + ".pdf");
Response.BinaryWrite(PdfContent);
Response.Flush();
return Content("");
}
catch (Exception ex)
{
throw this.log.CreatePropagatedException(ex);
}

Instead of using
return Content("")
use this:
return File(
PdfContent,
"application/pdf",
string.Format("BaseLine_Doc{0}.pdf", DocId)
)
There is no need to manipulate the response. Just return the correct result type.

Pulling out code from my running project
Response.Clear();
Response.AddHeader("Content-Disposition","attachment; filename=\"" + file[0].Name + "\"");
Response.AddHeader("Content-Length", fileSize.ToString(_culture));
Response.ContentType = Path.GetExtension(file[0].Name).GetMimeType();
Response.BufferOutput = false;
Response.AddHeader("Accept-Ranges", "bytes"); //tell the client that we accept byte-range requests
while (totalBytesRead < fileSize)
{
if (!Response.IsClientConnected)
{
System.Diagnostics.Debug.WriteLine("--> Client disconnected");
break;
}
int bytesRead = fs.Read(buffer, 0, buffer.Length);
Response.OutputStream.Write(buffer, 0, bytesRead);
Response.Flush();
totalBytesRead += bytesRead;
}
Response.End();
This code works for me every time, please manipulate according to your need.
Note: Variables are defined above the code.

Related

Returning a downloadable file using a stream in asp.net web forms

In asp.net MVC I can do something like the following which will open a stream:
Stream strm1 = GenerateReport(Id);
return File(strm1,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"Report_" + reportId.ToString() + ".xlsx");
Notice how I am passing strm1 which is a stream. I can then name it Report_+ ...xlsx like the example above shows.
Is there a similar way to do this with asp.net web forms using c#.
You can use TransmitFile or WriteFile if the file is in your website folder.
string fileName = string.Format("Report_{0}.xlsx", reportId);
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition",
string.Format("attachment; filename={0}", fileName));
Response.TransmitFile(fileName);
Response.End();
Stream
If your data is already in Memory, you want this method which writes the response in chunks.
Stream stm1 = GenerateReport(Id);
Int16 bufferSize = 1024;
byte[] buffer = new byte[bufferSize + 1];
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition",
string.Format("attachment; filename=\"Report_{0}.xlsx\";", reportId));
Response.BufferOutput = false;
int count = stm1.Read(buffer, 0, bufferSize);
while (count > 0)
{
Response.OutputStream.Write(buffer, 0, count);
count = stm1.Read(buffer, 0, bufferSize);
}
I use this extension to send a stream as a downloadable file:
public static class ToDownloadExtention
{
public static void ToDownload(this Stream stream, string fileName, HttpResponse response)
{
response.Clear();
response.ContentType = "application/octet-stream";
response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", fileName));
stream.CopyTo(response.OutputStream);
response.End();
}
}
And the usage is:
var stream = new MemoryStream();
stream.ToDownload("someFileName.ext",Response);
Or if you have a stream ready to be written, simply copy it to response stream:
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", "attachment; filename={your file name}");
Response.OutputStream.Write(stream, 0, stream.length);
Response.End();
Added same code just for visibility

Server.map path not working in asp.net

I am using this code to download a excel file which exist in my solution. I have added a folder FileUpload and added a excel file UploadCWF.xlsx. My code is workin in local host. But not working when I host this to server.I am getting error - Could not find a part of the path. My code -
string filePath = HttpContext.Current.Server.MapPath("~/FileUpload/");
string _DownloadableProductFileName = "UploadCWF.xlsx";
System.IO.FileInfo FileName = new System.IO.FileInfo(filePath + "\\" + _DownloadableProductFileName);
FileStream myFile = new FileStream(filePath + "\\" + _DownloadableProductFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
//Reads file as binary values
BinaryReader _BinaryReader = new BinaryReader(myFile);
//Check whether file exists in specified location
if (FileName.Exists)
{
try
{
long startBytes = 0;
string lastUpdateTiemStamp = File.GetLastWriteTimeUtc(filePath).ToString("r");
string _EncodedData = HttpUtility.UrlEncode(_DownloadableProductFileName, Encoding.UTF8) + lastUpdateTiemStamp;
Response.Clear();
Response.Buffer = false;
Response.AddHeader("Accept-Ranges", "bytes");
Response.AppendHeader("ETag", "\"" + _EncodedData + "\"");
Response.AppendHeader("Last-Modified", lastUpdateTiemStamp);
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName.Name);
Response.AddHeader("Content-Length", (FileName.Length - startBytes).ToString());
Response.AddHeader("Connection", "Keep-Alive");
Response.ContentEncoding = Encoding.UTF8;
//Send data
_BinaryReader.BaseStream.Seek(startBytes, SeekOrigin.Begin);
//Dividing the data in 1024 bytes package
int maxCount = (int)Math.Ceiling((FileName.Length - startBytes + 0.0) / 1024);
//Download in block of 1024 bytes
int i;
for (i = 0; i < maxCount && Response.IsClientConnected; i++)
{
Response.BinaryWrite(_BinaryReader.ReadBytes(1024));
Response.Flush();
}
}
catch (Exception es)
{
throw es;
}
finally
{
Response.End();
_BinaryReader.Close();
myFile.Close();
}
}
else
System.Web.UI.ScriptManager.RegisterStartupScript(this, GetType(),
"FileNotFoundWarning", "alert('File is not available now!')", true);
Please some one help me.
You should first concat filepath and filename then get path using server.mappath.
You should write code like this
string filePath = HttpContext.Current.Server.MapPath("~/FileUpload/UploadCWF.xlsx");
System.IO.FileInfo FileName = new System.IO.FileInfo(filePath);

Download all types of files from server using ASP.NET

I have a file in server .
I want download this file .
I use this code
try
{
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.BufferOutput = true;
if (File.Exists(Server.MapPath("~/Upload/" + file)))
{
Response.AddHeader("Content-Disposition", "attachment; filename=" + file);
Response.ContentType = "application/octet-stream";
Response.WriteFile(("~/Upload/" + file));
Response.End();
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
else
{
if (Request.UrlReferrer != null)
{
Type csType = GetType();
string jsScript = "alert('File Not Found');";
ScriptManager.RegisterClientScriptBlock(Page, csType, "popup", jsScript, true);
}
}
}
catch (Exception ex)
{
string errorMsg = ex.Message;
ScriptManager.RegisterClientScriptBlock(Page, GetType(), "popup", errorMsg, true);
}
But when i use this, I get error
Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.
in code
Response.End();
How to download all types of files?
Is this Web Forms, MVC, or a custom IHttpHandler?
In any event, you don't need to call Response.End() or HttpContext.Current.ApplicationInstance.CompleteRequest(), just return from your function and ensure nothing else is written to the response.
Have you tried :
Response.TransmitFile(Server.MapPath("~/Upload/" + file));
instead of :
Response.WriteFile(("~/Upload/" + file));
You should also remove the BufferOutput (bad idea with Response.End() without Response.Flush())

Downloading file on client side using absolute path .NET

string FilePath = HttpUtility.UrlDecode(Request.QueryString.ToString());
string[] s = FilePath.Split(new char[] { ',' });
string path = s[0];
string FileName = s[1];
String str = HttpContext.Current.Request.Url.AbsolutePath;
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(path+FileName);
response.Flush();
response.End();
Above is the code in which i get location of audio file from another page . the audio file is located on a remote machine which is accesible using url e.g. http:\servername\audiofiles\filename.wav . response.Transmit and .WriteFile requires virtual path whereas response.Write() does not download file . How can i give the absolute url instead of virtual path to download file
Found the answer my self from another place :
string FilePath = HttpUtility.UrlDecode(Request.QueryString.ToString());
string[] s = FilePath.Split(new char[] { ',' });
string path = s[0];
string FileName = s[1];
int bytesToRead = 10000;
byte[] buffer = new Byte[bytesToRead];
try
{
HttpWebRequest fileReq = (HttpWebRequest)HttpWebRequest.Create(path+FileName);
HttpWebResponse fileResp = (HttpWebResponse)fileReq.GetResponse();
if (fileReq.ContentLength > 0)
fileResp.ContentLength = fileReq.ContentLength;
stream = fileResp.GetResponseStream();
var resp = HttpContext.Current.Response;
resp.ContentType = "application/octet-stream";
resp.AddHeader("Content-Disposition", "attachment; filename=\"" + FileName + "\"");
resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());
int length;
do
{
if (resp.IsClientConnected)
{
// Read data into the buffer.
length = stream.Read(buffer, 0, bytesToRead);
// and write it out to the response's output stream
resp.OutputStream.Write(buffer, 0, length);
resp.Flush();
//Clear the buffer
buffer = new Byte[bytesToRead];
}
else
{
// cancel the download if client has disconnected
length = -1;
}
} while (length > 0); //Repeat until no data is read
}
finally
{
if (stream != null)
{
//Close the input stream
stream.Close();
}
}
}

How can I output to a webform before I clear a response?

I have a Webform where a user clicks a button and an Excel file is generated.
This is achieved by this code:
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=export.txt");
Response.ContentType = "text/csv";
Response.WriteFile(FILENAME);
Response.End();
I would like to add to the Response so when the user closes Excel, they can see a message on the Webform.But you can't do this in the code above.
Response.Write("Excel generated!"); ************ does not work as response will be cleared!
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=donman_export.txt");
Response.ContentType = "text/csv";
Response.WriteFile(FILENAME);
Response.End();
How can I do this?
Response.Write("Excel generated!"); ************ does not work
Response.Flush();
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=donman_export.txt");
Response.ContentType = "text/csv";
Response.WriteFile(FILENAME);
Response.End();
Code below is doing exactly what have you asked.
It renders additional HTML tags, so download starts after message was shown to user:
protected void Page_Load(object sender, EventArgs e)
{
var currentUrl = Request.Url.OriginalString;
var currentQuery = Request.Url.Query;
var download = new
{
FilePath = "~/test.csv",
FileName = "test.csv",
FileMime = "text/csv",
Message = "Excel generated!",
QueryParam = "direct-download",
Delay = 2 // seconds
};
var hasQueryParams = currentQuery.Length > 0;
var isDownloadUrl = hasQueryParams && currentQuery.IndexOf( download.QueryParam ) >= 0;
if( isDownloadUrl )
{
// Prepare..
Response.ContentType = download.FileMime;
Response.Clear();
Response.BufferOutput = true;
// Transfer..
Response.AddHeader("content-disposition", "attachment; filename=" + download.FileName);
Response.WriteFile(download.FilePath);
// Done..
// Instead of Response.Close()
// http://stackoverflow.com/q/4583201/2361743
Response.Flush();
Context.ApplicationInstance.CompleteRequest();
return;
}
// Meta-Refresh Tag has to be in <HEAD> section, but not all browsers follow this restriction.
// IFRAME has to work fine. It is wrapped into <DIV> to be not visible in old browsers.
const string tagToStartManual = "<A href='{0}'>{1}</A>";
const string tagToStartAfterDelay = "<META HTTP-EQUIV='REFRESH' CONTENT='{1};URL={0}'>";
const string tagToStartImmediately = "<DIV STYLE='{1}'><IFRAME SRC='{0}'></IFRAME></DIV>";
const string cssToHideFrame = "width:1px;height:1px;opacity:0.1;overflow:hidden";
// Show your message..
// And add HTML Tags which would start download:
Response.Write(download.Message);
var downloadUrl = currentUrl + (hasQueryParams ? "&" : "?") + download.QueryParam;
// You don't have to use all 3 methods...
Response.Write( String.Format( tagToStartManual, downloadUrl, download.FileName));
Response.Write( String.Format( tagToStartAfterDelay, downloadUrl, download.Delay) );
Response.Write( String.Format( tagToStartImmediately, downloadUrl, cssToHideFrame) );
// Done.
// Waiting for actual download request...
}

Resources