Show file in web browser - c#

I got a response which contains a PDF file
HttpWebResponse res = (HttpWebResponse)HttpWRequest.GetResponse();
Stream pdf = res.GetResponseStream();
I want to show this PDF on web browser without save it in a file. How could I do that?

I'm using the following code and works fine.
public static void SendDataByteFileToClient(HttpContext context, byte[] data, string fileName, string contentType, bool clearHeaders = true)
{
if (clearHeaders)
{
context.Response.Clear();
context.Response.ClearHeaders();
}
context.Response.BufferOutput = true;
context.Response.ContentType = contentType;
context.Response.AddHeader("Content-Disposition", "inline; filename=" + fileName);
context.Response.AddHeader("Content-Length", data.Length.ToString());
if (BrowserHelper.IsOfType(BrowserTypeEnum.IE) && BrowserHelper.Version < 9)
{
context.Response.Cache.SetCacheability(HttpCacheability.Private);
context.Response.Cache.SetMaxAge(TimeSpan.FromMilliseconds(1));
}
else
{
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);//IE set to not cache
context.Response.Cache.SetNoStore();//Firefox/Chrome not to cache
context.Response.Cache.SetExpires(DateTime.UtcNow); //for safe measure expire it immediately
}
if (data.Length > 0)
{
context.Response.BinaryWrite(data);
}
context.Response.End();
}

HttpWebResponse res = (HttpWebResponse)HttpWRequest.GetResponse();
Stream pdfdata = res.GetResponseStream();
string path = pdfdata.ToString();
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(path);
if (buffer != null)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
Response.End();
}

Related

ASP NET MVC download ZIP file

I'm trying to download a ZIP file in ASP NET MVC. I have done in ASP NET Webforms, and it works correcly, but I do the same in MVC and I don't get the same result, I tried the following:
public ActionResult Download()
{
using (ZipFile zip = new ZipFile())
{
zip.AddDirectory(Server.MapPath("~/Directories/hello"));
zip.Save(Server.MapPath("~/Directories/hello/sample.zip"));
return File(Server.MapPath("~/Directories/hello/sample.zip"),
"application/zip", "sample.zip");
}
}
But I get the binary data in screen, not the downloaded zip file why this is not working in MVC?
I have found that this does not work if I do it from a partial class, if I execute the download code from the Index and send the file if it works, why?
I use this to download files. In your view:
var ext = Path.GetExtension(path);
string contentType = GetMimeType(ext);
using (var stream = fileManager.GetStream(path))
{
var filename = fileManager.GetFileName(path);
var response = System.Web.HttpContext.Current.Response;
TransmitStream(stream, response, path, filename, contentType);
return new EmptyResult();
}
Where GetMimeType is a method that return known MIME types:
public static string GetMimeType(string extension, string defaultValue = "application/octet-stream")
{
if (extension == null)
{
throw new ArgumentNullException(nameof(extension));
}
if (!extension.StartsWith("."))
{
extension = "." + extension;
}
string mime;
return _mappings.TryGetValue(extension, out mime) ? mime : defaultValue;
}
With _mappings as:
private static readonly IDictionary<string, string> _mappings =
new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase) {
{".323", "text/h323"},
{".3g2", "video/3gpp2"},
{".3gp", "video/3gpp"},
{".3gp2", "video/3gpp2"},
{".3gpp", "video/3gpp"},
{".7z", "application/x-7z-compressed"},
// Other types...
{".xwd", "image/x-xwindowdump"},
{".z", "application/x-compress"},
{".zip", "application/x-zip-compressed"},
};
And the TransmitStream:
public static void TransmitStream(
Stream stream, HttpResponse response, string fullPath, string outFileName = null, string contentType = null)
{
contentType = contentType ?? MimeMapping.GetMimeMapping(fullPath);
byte[] buffer = new byte[10000];
try
{
var dataToRead = stream.Length;
response.Clear();
response.ContentType = contentType;
if (outFileName != null)
{
response.AddHeader("Content-Disposition", "attachment; filename=" + outFileName);
}
response.AddHeader("Content-Length", stream.Length.ToString());
while (dataToRead > 0)
{
// Verify that the client is connected.
if (response.IsClientConnected)
{
// Read the data in buffer.
var length = stream.Read(buffer, 0, 10000);
// Write the data to the current output stream.
response.OutputStream.Write(buffer, 0, length);
// Flush the data to the output.
response.Flush();
buffer = new byte[10000];
dataToRead = dataToRead - length;
}
else
{
// Prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
catch (Exception ex)
{
throw new ApplicationException(ex.Message);
}
finally
{
response.Close();
}
}
Usually, if you want to download something i suggest you to use ContentResult
,transforming the file you want to download into a Base64 String and transforming it on the frontend using javascript with a Blob
Action
public ContentResult Download()
{
MemoryStream memoryStream = new MemoryStream();
file.SaveAs(memoryStream);
byte[] buffer = memoryStream.ToArray();
string fileAsString = Convert.ToBase64String(buffer);
return Content(file, "application/zip");
}
front end
var blob = new Blob([Base64ToBytes(response)], { type: "application/zip" });
var link = document.createElement("a");
document.body.appendChild(link);
link.setAttribute("type", "hidden");
link.href = url.createObjectURL(blob);
link.download = fileName;
link.click();

File download in chunks in http-context response C#

I have a below scenario.
Client send request to Server-1 for file download
Server-1 send request to Server-2 for file.
To make this work I need to create a mechanism where once client send request to the Server-1, Server-1 will request to Server-2 which will send file as response output-stream in chunks. Server-1 will send this file chunks to client browser continuously as it keep receiving from server-2.
I have done code as below, theoretically it looks fine but still it is not working.
It is not downloading entire file in client browser, it seems like last chunk is not transferred to the Server-1 or it is not downloading to client browser from Server-1
Server-1 Code (Where client request for File download)
private void ProccesBufferedResponse(HttpWebRequest webRequest, HttpContext context)
{
char[] responseChars = null;
byte[] buffer = null;
if (webRequest == null)
logger.Error("Request string is null for Perfios Docs Download at ProccesBufferedResponse()");
context.Response.Buffer = false;
context.Response.BufferOutput = false;
try
{
WebResponse webResponse = webRequest.GetResponse();
context.Response.ContentType = "application/pdf";
context.Response.AddHeader("Content-disposition", webResponse.Headers["Content-disposition"]);
StreamReader responseStream = new StreamReader(webResponse.GetResponseStream());
while (!responseStream.EndOfStream)
{
responseChars = new char[responseStream.ToString().ToCharArray().Length];
responseStream.Read(responseChars, 0, responseChars.Length);
buffer = Encoding.ASCII.GetBytes(responseChars);
context.Response.Clear();
context.Response.OutputStream.Write(buffer, 0, buffer.Length);
context.Response.Flush();
}
}
catch (Exception ex)
{
throw;
}
finally
{
context.Response.Flush();
context.Response.End();
}
}
Server-2 Code (Where Server-1 will send request for file)
private void DownloadInstaPerfiosDoc(int CompanyID, string fileName, string Foldertype)
{
string folderPath;
string FilePath;
int chunkSize = 1024;
int startIndex = 0;
int endIndex = 0;
int length = 0;
byte[] bytes = null;
DirectoryInfo dir;
folderPath = GetDocumentDirectory(CompanyID, Foldertype);
FilePath = folderPath + "\\" + fileName;
dir = new DirectoryInfo(folderPath);
HttpContext.Current.Response.Buffer = false;
HttpContext.Current.Response.BufferOutput = false;
if (dir.Exists && dir.GetFiles().Length > 0)
{
foreach (var file in dir.GetFiles(fileName))
{
FilePath = folderPath + "\\" + file.Name;
FileStream fsReader = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("Content-disposition", string.Format("attachment; filename = \"{0}\"", fileName));
int totalChunks = (int)Math.Ceiling((double)fsReader.Length / chunkSize);
for (int i = 0; i < totalChunks; i++)
{
startIndex = i * chunkSize;
if (startIndex + chunkSize > fsReader.Length)
endIndex = (int)fsReader.Length;
else
endIndex = startIndex + chunkSize;
length = (int)endIndex - startIndex;
bytes = new byte[length];
fsReader.Read(bytes, 0, bytes.Length);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.OutputStream.Write(bytes, 0, bytes.Length);
HttpContext.Current.Response.Flush();
}
}
}
}
Please help me to resolve this issue.
It is possible and feasible. I'll give a pseudo procedure for you to understand the overall idea.
Server1
download action gets hit
create a request to server2
get the response stream of your server2 request
read the response stream in desired chunk sizes until it's consumed completely
write each chunk (as soon as you read) to current response stream
Server2
download action gets hit
write your stream onto your current response stream however you like

Save PDF file in Downloads folder in ASP .NET MVC

In my method in controller I use the following code to save pdf.
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlContent);
HtmlNode node = doc.GetElementbyId("DetailsToPDF");
HtmlToPdfConverter htmlToPdf = new HtmlToPdfConverter();
var pdfBytes = htmlToPdf.GeneratePdf("<html><body>" + node.InnerHtml + "</body></html>");
Response.ContentType = "application/pdf";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AddHeader("Content-Disposition", "attachment; filename=TEST.pdf");
Response.BinaryWrite(pdfBytes);
Response.Flush();
Response.End();
Everything has passed without any exceptions in debugger. However file is not saved. What am I doing wrong?
The recommended way to return a File in ASP.NET MVC is using the File() helper method:
public ActionResult Download()
{
// Starting with pdfBytes here...
// ...
var pdfBytes = htmlToPdf.GeneratePdf("<html><body>" + node.InnerHtml + "</body></html>");
var contentDisposition = new System.Net.Mime.ContentDisposition
{
FileName = "TEST.pdf",
Inline = false
};
Response.AppendHeader("Content-Disposition", contentDisposition.ToString());
return File(pdfBytes, "application/pdf");
}
string path = Server.MapPath("~/Content/files/newPDFFile.pdf");
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(path);
if (buffer != null)
{
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + "PDFfile.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(buffer);
Response.End();
}

imagehandler.ashx image doesn't display in chrome

imagehandler.ashx image doesn't display in Chrome browser. How can I fix it..?
My Codes (imagehandler.ashx):
public void ProcessRequest(HttpContext context)
{
if (context.Request.QueryString["YazarID"] != null)
{
string YazarID = context.Request.QueryString["YazarID"];
DataTable dt = new DataTable();
string query = "select img from Register where YazarID='" + YazarID + "'";
dt = Database.GetData(query);
HttpResponse r = context.Response;
r.WriteFile("../Pictures/300/" + dt.Rows[0]["img"]);
HttpContext.Current.ApplicationInstance.CompleteRequest();
context.Response.Flush();
context.Response.Close();
context.Response.End();
}
}
images looking like this in Chrome browser;
You are not sending the content-Length. It could mess up the images (and other files) in Chrome. Assuming the file is correctly saved in the database of course.
public void ProcessRequest(HttpContext context)
{
//create a new byte array
byte[] bin = new byte[0];
//get the item from a datatable
bin = (byte[])dt.Rows[0]["img"];
//read the image in an `Image` and then get the bytes in a memorystream
Image img = Image.FromFile(context.Server.MapPath("test.jpg"));
using (var ms = new MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
bin = ms.ToArray();
}
//or as one-liner
bin = File.ReadAllBytes(context.Server.MapPath("test.jpg"));
//clear the buffer stream
context.Response.ClearHeaders();
context.Response.Clear();
context.Response.Buffer = true;
//set the correct ContentType
context.Response.ContentType = "image/jpeg";
//set the filename for the image
context.Response.AddHeader("Content-Disposition", "attachment; filename=\"myImage.jpg\"");
//set the correct length of the string being send
context.Response.AddHeader("content-Length", bin.Length.ToString());
//send the byte array to the browser
context.Response.OutputStream.Write(bin, 0, bin.Length);
//cleanup
context.Response.Flush();
context.ApplicationInstance.CompleteRequest();
}

How to download txt file from the server system to client?

<%# WebHandler Language="C#" Class="dlde" %>
using System;
using System.Web;
public class dlde : IHttpHandler {
public void ProcessRequest (HttpContext context) {
var fileName = #"D:\Error.txt";
var r = context.Response;
r.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
r.ContentType = "text/plain";
r.WriteFile(context.Server.MapPath(fileName));
}
public bool IsReusable {
get {
return false;
}
}
}
I want to download the .txt file from the server system from the specified directory...i tried with this code,but i am getting error,invalid virtual path..How to get the file ,from the server system ..please help me...
try this by using "WebClient "
string remoteUri = "http://Yoursite.com/file.txt";
string fileName = #"D:\";
WebClient myWebClient = new WebClient();
myWebClient.DownloadFile(remoteUri, fileName);
Learning from MSDN for namespace
string fName = #"D:\Error.txt";
System.IO.FileStream fs = System.IO.File.Open(fName, System.IO.FileMode.Open);
byte[] btFile = new byte[fs.Length];
fs.Read(btFile, 0, Convert.ToInt32(fs.Length));
fs.Close();
context.Response.Clear();
context.Response.ClearHeaders();
context.Response.AddHeader("Content-disposition", "attachment;
filename=" + HttpUtility.UrlEncode(fname, System.Text.Encoding.UTF8));
context.Response.ContentType = "text/plain";
context.Response.BinaryWrite(btFile);
string fName = #hdfRuta.Value; //Ruta donde se encuentra el archivo txt Ejm: c:\log\log21082018.txt
System.IO.FileStream fs = System.IO.File.Open(fName,
System.IO.FileMode.Open);
byte[] btFile = new byte[fs.Length];
fs.Read(btFile, 0, Convert.ToInt32(fs.Length));
fs.Close();
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment; filename=" + hdfNombreArchivo.Value); //hdfNombreArchivo: Nombre del archivo .txt a generarse Ejm: Log_21_08_2018.txt
EnableViewState = false;
Response.ContentType = "text/plain";
Response.BinaryWrite(btFile);
Response.End();
protected void btnBajarLog_PreRender(object sender, EventArgs e)
{
Button btn = (Button)sender;
ScriptManager newScriptManager =
(ScriptManager)Page.FindControl("scmRegistroMasivoEvento");
newScriptManager.RegisterPostBackControl(btn);
}

Categories

Resources