Unable to open download save dialog - c#

Using the below code I am unable to show the open/save as file dialog:
public void ProcessRequest(HttpContext context)
{
string link = context.Request.QueryString["Link"];
string extension = Path.GetExtension(link);
string fileName = Path.GetFileName(link);
string fullPath =
String.Format("{0}\\{1}",
context.Server.MapPath("~/Content/Uploads/"),
fileName);
if (File.Exists(fullPath))
{
context.Response.ClearContent();
context.Response.ClearHeaders();
context.Response.AddHeader(
"Content-Length",
new FileInfo(fullPath).Length.ToString());
string contentType;
switch (extension)
{
default:
contentType = "application/octet-stream";
break;
}
context.Response.ContentType = contentType;
context.Response.AddHeader(
"Content-Disposition",
String.Format("attachment; filename={0}", fileName));
context.Response.WriteFile(fullPath, true);
context.Response.Flush();
}
}
I've tried to close the response, leave the response open, use TrasmitFile(), but I never get any dialog or any feed back whatsoever. I've tried debugging it as well, but no exceptions are being thrown. Tried in IE 7/8, and Chrome. Any help is appreciated.
Thanks!
Below is the Fiddler output:
HTTP/1.1 200 OK Cache-Control: private
Content-Length: 3813 Content-Type:
application/octet-stream Server:
Microsoft-IIS/7.5 Content-Disposition:
attachment;
filename=b1af9b34-28cc-4479-a056-8c55b41a5ece.txt
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET Date: Thu, 23
Dec 2010 21:51:58 GMT
* Home
* Hotels
* Reviews
* Community
* Travel Guide
* Travel Insurance
* Contact us
* FIDDLER: RawDisplay truncated at 128 characters. Right-click to disable
truncation. *

Finally figured it out. There is actually no problem with the code I posted. As you can see in the Fiddler output, the contents of the text file were successfully written to the response stream and the headers used were also correct. The actual problem comes from how the actual http request was made. I used a
$.get(urlToGenericHandler);
request using jQuery. The reason why specifically I am not able to download a file using AJAX or a callback model is beyond the scope of this answer. See supported jQuery datatypes here
Anyways, I changed the call from using AJAX to using a basic post-back.
Thanks to all that helped.

Try changing
contentType = "application/octet-stream";
to
contentType = "application/download";
Update:
Try swapping the position of the header and content type
context.Response.AddHeader(
"Content-Disposition",
String.Format("attachment; filename={0}", fileName));
context.Response.ContentType = contentType;
context.Response.AddHeader(
"Content-Length",
new FileInfo(fullPath).Length.ToString());

Related

how to read a PDF into a char array for HTTP response output [duplicate]

I have an app that needs to read a PDF file from the file system and then write it out to the user. The PDF is 183KB and seems to work perfectly. When I use the code at the bottom the browser gets a file 224KB and I get a message from Acrobat Reader saying the file is damaged and cannot be repaired.
Here is my code (I've also tried using File.ReadAllBytes(), but I get the same thing):
using (FileStream fs = File.OpenRead(path))
{
int length = (int)fs.Length;
byte[] buffer;
using (BinaryReader br = new BinaryReader(fs))
{
buffer = br.ReadBytes(length);
}
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", String.Format("attachment;filename={0}", Path.GetFileName(path)));
Response.ContentType = "application/" + Path.GetExtension(path).Substring(1);
Response.BinaryWrite(buffer);
}
Try adding
Response.End();
after the call to Response.BinaryWrite().
You may inadvertently be sending other content back after Response.BinaryWrite which may confuse the browser. Response.End will ensure that that the browser only gets what you really intend.
Response.BinaryWrite(bytes);
Response.Flush();
Response.Close();
Response.End();
This works for us. We create PDFs from SQL Reporting Services.
We've used this with a lot of success. WriteFile do to the download for you and a Flush / End at the end to send it all to the client.
//Use these headers to display a saves as / download
//Response.ContentType = "application/octet-stream";
//Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}.pdf", Path.GetFileName(Path)));
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", String.Format("inline; filename={0}.pdf", Path.GetFileName(Path)));
Response.WriteFile(path);
Response.Flush();
Response.End();
Since you're sending the file directly from your filesystem with no intermediate processing, why not use Response.TransmitFile instead?
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition",
"attachment; filename=\"" + Path.GetFileName(path) + "\"");
Response.TransmitFile(path);
Response.End();
(I suspect that your problem is caused by a missing Response.End, meaning that you're sending the rest of your page's content appended to the PDF data.)
Just for future reference, as stated in this blog post:
http://blogs.msdn.com/b/aspnetue/archive/2010/05/25/response-end-response-close-and-how-customer-feedback-helps-us-improve-msdn-documentation.aspx
It is not recommended to call Response.Close() or Response.End() - instead use CompleteRequest().
Your code would look somewhat like this:
byte[] bytes = {};
bytes = GetBytesFromDB(); // I use a similar way to get pdf data from my DB
Response.Clear();
Response.ClearHeaders();
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + anhangTitel);
Response.AppendHeader("Content-Length", bytes.Length.ToString());
this.Context.ApplicationInstance.CompleteRequest();
Please read this before using Response.TransmitFile: http://improve.dk/blog/2008/03/29/response-transmitfile-close-will-kill-your-application
Maybe you are missing a Response.close to close de Binary Stream
In my MVC application, I have enabled gzip compression for all responses. If you are reading this binary write from an ajax call with gzipped responses, you are getting the gzipped bytearray rather than original bytearray that you need to work with.
//c# controller is compressing the result after the response.binarywrite
[compress]
public ActionResult Print(int id)
{
...
var byteArray=someService.BuildPdf(id);
return return this.PDF(byteArray, "test.pdf");
}
//where PDF is a custom actionresult that eventually does this:
public class PDFResult : ActionResult
{
...
public override void ExecuteResult(ControllerContext context)
{
//Set the HTTP header to excel for download
HttpContext.Current.Response.Clear();
//HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", string.Concat("attachment; filename=", fileName));
HttpContext.Current.Response.AddHeader("Content-Length", pdfBytes.Length.ToString());
//Write the pdf file as a byte array to the page
HttpContext.Current.Response.BinaryWrite(byteArray);
HttpContext.Current.Response.End();
}
}
//javascript
function pdf(mySearchObject) {
return $http({
method: 'Post',
url: '/api/print/',
data: mySearchObject,
responseType: 'arraybuffer',
headers: {
'Accept': 'application/pdf',
}
}).then(function (response) {
var type = response.headers('Content-Type');
//if response.data is gzipped, this blob will be incorrect. you have to uncompress it first.
var blob = new Blob([response.data], { type: type });
var fileName = response.headers('content-disposition').split('=').pop();
if (window.navigator.msSaveOrOpenBlob) { // for IE and Edge
window.navigator.msSaveBlob(blob, fileName);
} else {
var anchor = angular.element('<a/>');
anchor.css({ display: 'none' }); // Make sure it's not visible
angular.element(document.body).append(anchor); // Attach to document
anchor.attr({
href: URL.createObjectURL(blob),
target: '_blank',
download: fileName
})[0].click();
anchor.remove();
}
});
}
" var blob = new Blob([response.data], { type: type }); "
This will give you that invalid/corrupt file that you are trying to open when you turn that byte array into a file in your javascript if you don't uncompress it first.
To fix this, you have a choice to either prevent gzipping this binary data so that you can properly turn it into the file that you are downloading, or you have to decompress that gzipped data in your javascript code before you turn it into a file.
In addition to Igor's Response.Close(), I would add a Response.Flush().
I also found it necessary to add the following:
Response.Encoding = Encoding.Default
If I didn't include this, my JPEG was corrupt and double the size in bytes.
But only if the handler was returning from an ASPX page. It seemed running from an ASHX this was not required.

Video streaming from database

I am trying to make an action to display media from database (ASP.NET MVC4):
var memoryStream = new MemoryStream(mediaContent.File.FileData.Data);
return new FileStreamResult(memoryStream, MimeMapping.GetMimeMapping(mediaContent.File.Filename));
Pictures are displaying fine, but I have problem with videos (.avi) when I am going to link mysite/media/4 in Chrome or Firefox it displays:
<embed width="100%" height="100%" name="plugin" src="http://mysite/media/4" type="video/x-msvideo">
But video is not playing (as it happens if link pointing to real video file), but if I am opening this link in IE it prompts me to download file and when I am opening this file from player it works fine.
Response headers:
Cache-Control:private, s-maxage=0
Content-Length:808680
Content-Type:video/x-msvideo
Date:Wed, 06 Nov 2013 10:03:09 GMT
Server:Microsoft-IIS/7.5
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:4.0
X-MiniProfiler-Ids:["e305dcdb-79be-4452-94d2-a9999ffaa13a","c0c81d12-8b31-425c-a57b-2ad186c958d5","1f7f3c09-a695-49f1-9203-6b5bf44b837a","fb0d637e-5926-4759-ad6f-f7322403e98c","f08c0392-10d6-4477-b2df-be52ab9a1d64","366d6122-15a5-41b4-840a-607fc6931996","11fd2eb7-efce-47a1-96f8-09fbdb0b1fa0","690e67b7-b1fb-46a3-9aa3-ef6207203f55","a51640ad-f31d-4f12-a807-6ea06ba0ee46","38adc052-9c41-4243-97d2-41dbf3b36093","9d255225-c122-44ef-8021-5b6f9d4dd549","2b249ff3-9e37-43c3-b6ab-b78b26c6d6ce","2bec0b1b-4898-4b14-bf12-cc331e27ecfc","49c72e01-c8d4-495f-af7e-8ffd687e94e9","1c87e454-f90d-49f4-9618-8dfe0d9c0329","2152a9a8-54ae-47d8-b98a-83ac32dbdb0c","9cf93254-9552-4834-826e-df7e8a7d8e73","a2d782e2-96ca-4e9c-b612-9782a37a06ca","e10ecc8a-5811-4cca-b566-3f09e1de3f2c","3769bb15-60f9-43c3-ad6c-285f3fb47112","1996c4aa-9f76-4f33-95fa-3f7f5b3e72f4"]
X-Powered-By:ASP.NET
What I am doing wrong? I want to have a link which I can use in <object> in order to display this video on page.
Update 1:
The response that I am getting if I type url for physical video file doesn't make any sence:
HTTP/1.1 304 Not Modified
Last-Modified: Tue, 05 Nov 2013 17:07:56 GMT
Accept-Ranges: bytes
ETag: "5d50369249dace1:0"
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Wed, 06 Nov 2013 11:20:32 GMT
But it works and start play video inside browser using intaled VLC player plugin.
Update 2:
I have tried different implementations to return video and tried to put link to src of object
<object id="video-player" class="preview-container" type="video/x-msvideo" src="{link to video}" loop="true" controls="false" autoplay="true"></object>
So in case if link to real video like "localhost/media/some_video.avi"
then it works fine inside object and if going to url directly.
I checked behavior for different implementations
1) return File(memoryStream, MimeMapping.GetMimeMapping(mediaContent.File.Filename), mediaContent.File.Filename);
to url: prompts to download video file;
in object: shows empty plugin;
2) return new FileContentResult(mediaContent.File.FileData.Data, "application/x-vlc-plugin")
to url: shows empty plugin;
in object: shows empty plugin;
3) return new FileContentResult(mediaContent.File.FileData.Data, "video/avi")
to url: prompts to download video file;
in object: shows empty plugin;
Update 3:
I made a HttpHandler:
public void ProcessRequest(HttpContext context)
{
MediaContent mediaContent;
//getting mediaContent
context.Response.Clear();
context.Response.Buffer = true;
context.Response.AppendHeader("Content-Disposition", "inline; filename=" + mediaContent.File.Filename);
context.Response.ContentType = MimeMapping.GetMimeMapping(mediaContent.File.Filename);
context.Response.BinaryWrite(mediaContent.File.FileData.Data);
context.Response.End();
}
And it simply WORKS. Ok. Then I made action with same logic as Handler:
[HttpGet]
public void Media(int id)
{
MediaContent mediaContent;
//getting mediaContent
Response.Clear();
Response.Buffer = true;
Response.AppendHeader("Content-Disposition", "inline; filename=" + mediaContent.File.Filename);
Response.ContentType = MimeMapping.GetMimeMapping(mediaContent.File.Filename);
Response.BinaryWrite(mediaContent.File.FileData.Data);
Response.End();
}
But this action still not working with video and I started to compare response headers.
Handler:
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: video/x-msvideo
Server: Microsoft-IIS/7.5
Content-Disposition: inline; filename=Reebok_App_attract640L.avi
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 06 Nov 2013 17:02:45 GMT
Content-Length: 808680
Action:
HTTP/1.1 200 OK
Cache-Control: private, s-maxage=0
Content-Type: video/x-msvideo
Transfer-Encoding: chunked
Server: Microsoft-IIS/7.5
X-AspNetMvc-Version: 4.0
Content-Disposition: inline; filename=Reebok_App_attract640L.avi
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 06 Nov 2013 17:02:47 GMT
To get rid of Transfer-Encoding I have added Content-Length to my action:
Response.AddHeader("Content-Length", mediaContent.File.FileData.Data.Length.ToString());
I couldn't get rid of s-maxage=0, but now headers similar (except s-maxage=0, X-AspNetMvc-Version: 4.0 and header order)
Well, I did my best to mirror your initial code, minus the database, what I have below works fine for me. VS2012, MVC4, IIS Express local, IIS7.5 remote, works on IE10 local, IE10 remote, IE9 remote. The AVI file I used is some random file I found inside my windows\winsxs folder. I am going to suggest you have a client side problem (IE specifically). Maybe something like cookie handling issues (http://mvolo.com/iis-70-forms-authentication-and-embedded-media-players/), IE security zone settings or something else?
By the way height=100% on embed does not work for me, needs to be pixels.
Controller
namespace MvcApplication4.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return new ViewResult();
}
public ActionResult Media(int id)
{
string fn = Server.MapPath("~/App_Data/boxed-delete.avi");
var memoryStream = new MemoryStream(System.IO.File.ReadAllBytes(fn));
return new FileStreamResult(memoryStream, MimeMapping.GetMimeMapping(System.IO.Path.GetFileName(fn)));
}
}
}
View
#{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
<!-- http://localhost:54941/Home/Media/3 -->
<embed width="100%" height="500" name="plugin" src="~/Home/Media/3" type="video/x-msvideo">
</div>
</body>
</html>
You can put file location stored in the database into a byte buffer array and read from the array in chunk instead of reading whole array at once
WebRequest wreq = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse())
using (Stream mystream = wresp.GetResponseStream())
{
using (BinaryReader reader = new BinaryReader(mystream))
{
int length = Convert.ToInt32(wresp.ContentLength);
byte[] buffer = new byte[length];
buffer = reader.ReadBytes(length);
Response.Clear();
Response.Buffer = false;
Response.ContentType = "video/mp4";
while(true) {
int bytesRead = myStream.Read(buffer, 0, buffer.Length);
if (bytesRead == 0) break;
Response.OutputStream.Write(buffer, 0, bytesRead);
}
Response.End();
}
}
Also you can check out the given url weather it helps,
http://www.devcurry.com/2013/04/play-videos-in-aspnet-mvc-html5-using.html
Thanks

Redirect response to download file

I have a generic handler that I call using this very basic js code:
var formData = new FormData();
formData.append("fileId", this.model.get("id"));
xhr = new XMLHttpRequest();
xhr.open('POST', '/genericHandlers/DownloadFile.ashx');
xhr.onload = function () {
if (xhr.status === 200) {
// Do something here?
}
}
xhr.send(formData);
My generic handler code seems to look ok to me. Basically I'm attempting to build the response header and I thought that when this handler returned it would begin the download of the file.
Handler code:
var fileId = context.Request.Form["fileId"];
// File stored in the db as a byte array
var file = (from f in dataContext.OneEVA_Docs_File_Storages
where Equals(f.ID, fileId)
select f).FirstOrDefault();
context.Response.Clear();
context.Response.AddHeader("Content-Type", file.ContentType);
context.Response.AddHeader("Content-Length", file.ContentLength.ToString());
context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}; size={1}", file.Name, file.ContentLength));
context.Response.BinaryWrite(file.File_Image.ToArray());
context.Response.Flush();
context.Response.End();
The XHR request completes ok. Here is the response header I get back:
Cache-Control:private
Connection:Close
Content-Disposition:attachment; filename=Mikes File; size=1860113
Content-Length:1860113
Content-Type:image/jpeg
Date:Wed, 16 May 2012 14:08:01 GMT
Server:ASP.NET Development Server/10.0.0.0
X-AspNet-Version:4.0.30319
What am I missing? What am I doing wrong?
That won't start a download in the browser. It's difficult to achieve using a POST request. Can it not be a GET request if you are only passing an id? Then you just do
document.location.href = '/genericHandlers/DownloadFile.ashx?id=' + this.model.get("id");

Big files uploading (WebException: The connection was closed unexpectedly)

UPDATED
See post #3 below.
There is a need to upload a file to the web automatically (without browser). Host - Mini File Host v1.2 (if this does matter). Didn't find specific api in documentation, so at first i sniffed browser requests in Firebug as follows :
Params : do
Value : verify
POST /upload.php?do=verify HTTP/1.1
Host: webfile.ukrwest.net
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; ru; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8 (.NET CLR 4.0.20506)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: ru,en-us;q=0.7,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://filehoster.awardspace.com/index.php
Content-Type: multipart/form-data; boundary=---------------------------27368237179714
Content-Length: 445
-----------------------------27368237179714
Content-Disposition: form-data; name="upfile"; filename="Test.file"
Content-Type: application/octet-stream
12345678901011121314151617sample text
-----------------------------27368237179714
Content-Disposition: form-data; name="descr"
-----------------------------27368237179714
Content-Disposition: form-data; name="pprotect"
-----------------------------27368237179714--
Here we can see parameter, headers, content type and chunks of information (1 - file name and type, 2 - file contents, 3 - additional params - description and password, not necessarily applied).
So i've created a class that emulates such a behaviour step by step : HttpWebRequest on the url, apply needed parameters to request, form request strings with StringBuilder and convert them to byte arrays, read a file using FileStream, putting all that stuff to MemoryStream and then writing it to request (took major part of a code from an article at CodeProject where it uploads a file to Rapidshare host).
Neat and tidy, but... It doesn't seem to work :(. As result it returns initial upload page, not a result page with links i could parse and present to a user...
Here are main methods of an Uploader class :
// Step 1 - request creation
private HttpWebRequest GetWebrequest(string boundary)
{
Uri uri = new Uri("http://filehoster.awardspace.com/index.php?do=verify");
System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
httpWebRequest.CookieContainer = _cookies;
httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
httpWebRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.2; ru; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8 (.NET CLR 4.0.20506)";
httpWebRequest.Referer = "http://filehoster.awardspace.com/index.php";
httpWebRequest.Method = "POST";
httpWebRequest.KeepAlive = true;
httpWebRequest.Timeout = -1;
//httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
httpWebRequest.Headers.Add("Accept-Charset", "windows-1251,utf-8;q=0.7,*;q=0.7");
httpWebRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
httpWebRequest.Headers.Add("Accept-Language", "ru,en-us;q=0.7,en;q=0.3");
//httpWebRequest.AllowAutoRedirect = true;
//httpWebRequest.ProtocolVersion = new Version(1,1);
//httpWebRequest.SendChunked = true;
//httpWebRequest.Headers.Add("Cache-Control", "no-cache");
//httpWebRequest.ServicePoint.Expect100Continue = false;
return httpWebRequest;
}
// Step 2 - first message part (before file contents)
private string GetRequestMessage(string boundary, string FName, string description, string password)
{
System.Text.StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("--");
stringBuilder.Append(boundary);
stringBuilder.Append("\r\n");
stringBuilder.Append("Content-Disposition: form-data; name=\"");
stringBuilder.Append("upfile");
stringBuilder.Append("\"; filename=\"");
stringBuilder.Append(FName);
stringBuilder.Append("\"");
stringBuilder.Append("\r\n");
stringBuilder.Append("Content-Type: application/octet-stream");
stringBuilder.Append("\r\n");
return stringBuilder.ToString();
}
// Step 4 - additional request parameters. Step 3 - reading file is in method below
private string GetRequestMessageEnd(string boundary)
{
System.Text.StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(boundary);
stringBuilder.Append("\r\n");
stringBuilder.Append("Content-Disposition: form-data; name=\"descr\"");
stringBuilder.Append("\r\n");
stringBuilder.Append("\r\n");
stringBuilder.Append("Default description");
stringBuilder.Append("\r\n");
stringBuilder.Append(boundary);
stringBuilder.Append("\r\n");
stringBuilder.Append("Content-Disposition: form-data; name=\"pprotect\"");
stringBuilder.Append("\r\n");
stringBuilder.Append("\r\n");
stringBuilder.Append("");
stringBuilder.Append("\r\n");
stringBuilder.Append(boundary);
stringBuilder.Append("--");
//stringBuilder.Append("\r\n");
//stringBuilder.Append(boundary);
//stringBuilder.Append("\r\n");
return stringBuilder.ToString();
}
// Main method
public string ProcessUpload(string FilePath, string description, string password)
{
// Chosen file information
FileSystemInfo _file = new FileInfo(FilePath);
// Random boundary generation
DateTime dateTime2 = DateTime.Now;
long l2 = dateTime2.Ticks;
string _generatedBoundary = "----------" + l2.ToString("x");
// Web request creation
System.Net.HttpWebRequest httpWebRequest = GetWebrequest(_generatedBoundary);
// Main app block - form and send request
using (System.IO.FileStream fileStream = new FileStream(_file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
byte[] bArr1 = Encoding.ASCII.GetBytes("\r\n--" + _generatedBoundary + "\r\n");
// Generating pre-content post message
string firstPostMessagePart = GetRequestMessage(_generatedBoundary, _file.Name, description, password);
// Writing first part of request
byte[] bArr2 = Encoding.UTF8.GetBytes(firstPostMessagePart);
Stream memStream = new MemoryStream();
memStream.Write(bArr1, 0, bArr1.Length);
memStream.Write(bArr2, 0, bArr2.Length);
// Writing file
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
memStream.Write(buffer, 0, bytesRead);
}
// Generating end of a post message
string secondPostMessagePart = GetRequestMessageEnd(_generatedBoundary);
byte[] bArr3 = Encoding.UTF8.GetBytes(secondPostMessagePart);
memStream.Write(bArr3, 0, bArr3.Length);
// Preparing to send
httpWebRequest.ContentLength = memStream.Length;
fileStream.Close();
Stream requestStream = httpWebRequest.GetRequestStream();
memStream.Position = 0;
byte[] tempBuffer = new byte[memStream.Length];
memStream.Read(tempBuffer, 0, tempBuffer.Length);
memStream.Close();
// Sending request
requestStream.Write(tempBuffer, 0, tempBuffer.Length);
requestStream.Close();
}
// Delay (?)
System.Threading.Thread.Sleep(5000);
// Getting response
string strResponse = "";
using (Stream stream = httpWebRequest.GetResponse().GetResponseStream())
using (StreamReader streamReader = new StreamReader(stream/*, Encoding.GetEncoding(1251)*/))
{
strResponse = streamReader.ReadToEnd();
}
return strResponse;
}
Plays with ProtocolVersion (1.0, 1.1), AllowAutoRedirect (true/false), even known ServicePoint.Expect100Continue (false) didn't fix an issue. Even a 5sec timeout before getting response (thought in case of a big file it doesn't uploads so quick) didn't help.
Content type "octet-stream" was chosen by purpose to upload any file (could use some switch for most popular jpg/zip/rar/doc etc., but that one seems universal). Boundary is generated randomly from timer ticks, not a big deal. What else? :/
I could give up and forget this, but i feel i'm pretty close to solve and then forget about it :P.
In case you need the whole application to run and debug - here it is (70kb, zipped C# 2.0 VS2k8 solution, no ads, no viruses) :
#Mediafire
#FileQube
#FileDropper
Update : nope, there is no redirect.
screenshot
Read RFC2388 few times, rewrote the code and it finally worked (i guess the trouble was in utf-read trailing boundary instead of correct 7 bit ascii). Hooray? Nope :(. Only small files are transfered, big ones throwing "The connection was closed unexpectedly".
System.Net.WebException was unhandled by user code
Message="The underlying connection was closed: The connection was closed unexpectedly."
Source="Uploader"
StackTrace:
at Uploader.Upload.ProcessUpload(String FilePath, String description, String password) in F:\MyDocuments\Visual Studio 2008\Projects\Uploader\Uploader.cs:line 96
at Uploader.Form1.backgroundWorker1_DoWork(Object sender, DoWorkEventArgs e) in F:\MyDocuments\Visual Studio 2008\Projects\Uploader\Form1.cs:line 45
at System.ComponentModel.BackgroundWorker.WorkerThreadStart(Object argument)
I know that's a bug with .net stack and few solutions exists :
increase both Timeout and ReadWriteTimeout of a request
assign request.KeepAlive = false and System.Net.ServicePointManager.Expect100Continue = false
set ProtocolVersion to 1.0
But neither one of them nor all of them altogether help in my case. Any ideas?
EDIT - Source code:
// .. request created, required params applied
httpWebRequest.ProtocolVersion = HttpVersion.Version10; // fix 1
httpWebRequest.KeepAlive = false; // fix 2
httpWebRequest.Timeout = 1000000000; // fix 3
httpWebRequest.ReadWriteTimeout = 1000000000; // fix 4
// .. request processed, data written to request stream
string strResponse = "";
try
{
using (WebResponse httpResponse = httpWebRequest.GetResponse()) // error here
{
using (Stream responseStream = httpResponse.GetResponseStream())
{
using (StreamReader streamReader = new StreamReader(responseStream))
{
strResponse = streamReader.ReadToEnd();
}
}
}
}
catch (WebException exception)
{
throw exception;
}
"As result it returns initial upload page, not a result page with links i could parse and present to a user..."
Maybe that's just the behaviour of the upload functionality: that after the upload has finished, you can upload another file?
I think you have to call another url for the "browse for file"-page (I suppose that's the one you need).
Edit: Actually, if the server sends a "redirect" (http 3xx), that's something the browser has to handle, so if you're working with your own client application in stead of a browser, you'll have to implement redirection yourself. Here the rfc for more information.
Try setting the maxRequestLength property of the httpRuntime element in the Web.config.
In my case, duplicate filename causing the issue as well. I save the file's settings in an xml file but the name setting is duplicating each other.
<field name="StillImage">
<prefix>isp_main_</prefix>
<suffix>308</suffix>
<width>1080</width>
<height>1080</height>
</field>
<field name="ThumbnailImage">
<prefix>isp_thumb_</prefix> // pay attention to this
<suffix>308</suffix>
<width>506</width>
<height>506</height>
</field>
<field name="Logo">
<prefix>isp_thumb_</prefix> // and this
<suffix>306</suffix>
<width>506</width>
<height>506</height>
</field>
And, in the other case I had, the issue is in the file length. Please do check the allowed file size on your server. In your script just do check this part :
dataStream.Write(filesBytesArray, 0, filesBytesArray.Length);
dataStream.Close();
And if you dont know, just limit the file uploaded size in your frontend section ie. HTML <input type="file"> upload element, this is good reference for limiting file size and other filter.

Reading a binary file and using Response.BinaryWrite()

I have an app that needs to read a PDF file from the file system and then write it out to the user. The PDF is 183KB and seems to work perfectly. When I use the code at the bottom the browser gets a file 224KB and I get a message from Acrobat Reader saying the file is damaged and cannot be repaired.
Here is my code (I've also tried using File.ReadAllBytes(), but I get the same thing):
using (FileStream fs = File.OpenRead(path))
{
int length = (int)fs.Length;
byte[] buffer;
using (BinaryReader br = new BinaryReader(fs))
{
buffer = br.ReadBytes(length);
}
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", String.Format("attachment;filename={0}", Path.GetFileName(path)));
Response.ContentType = "application/" + Path.GetExtension(path).Substring(1);
Response.BinaryWrite(buffer);
}
Try adding
Response.End();
after the call to Response.BinaryWrite().
You may inadvertently be sending other content back after Response.BinaryWrite which may confuse the browser. Response.End will ensure that that the browser only gets what you really intend.
Response.BinaryWrite(bytes);
Response.Flush();
Response.Close();
Response.End();
This works for us. We create PDFs from SQL Reporting Services.
We've used this with a lot of success. WriteFile do to the download for you and a Flush / End at the end to send it all to the client.
//Use these headers to display a saves as / download
//Response.ContentType = "application/octet-stream";
//Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}.pdf", Path.GetFileName(Path)));
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", String.Format("inline; filename={0}.pdf", Path.GetFileName(Path)));
Response.WriteFile(path);
Response.Flush();
Response.End();
Since you're sending the file directly from your filesystem with no intermediate processing, why not use Response.TransmitFile instead?
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition",
"attachment; filename=\"" + Path.GetFileName(path) + "\"");
Response.TransmitFile(path);
Response.End();
(I suspect that your problem is caused by a missing Response.End, meaning that you're sending the rest of your page's content appended to the PDF data.)
Just for future reference, as stated in this blog post:
http://blogs.msdn.com/b/aspnetue/archive/2010/05/25/response-end-response-close-and-how-customer-feedback-helps-us-improve-msdn-documentation.aspx
It is not recommended to call Response.Close() or Response.End() - instead use CompleteRequest().
Your code would look somewhat like this:
byte[] bytes = {};
bytes = GetBytesFromDB(); // I use a similar way to get pdf data from my DB
Response.Clear();
Response.ClearHeaders();
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + anhangTitel);
Response.AppendHeader("Content-Length", bytes.Length.ToString());
this.Context.ApplicationInstance.CompleteRequest();
Please read this before using Response.TransmitFile: http://improve.dk/blog/2008/03/29/response-transmitfile-close-will-kill-your-application
Maybe you are missing a Response.close to close de Binary Stream
In my MVC application, I have enabled gzip compression for all responses. If you are reading this binary write from an ajax call with gzipped responses, you are getting the gzipped bytearray rather than original bytearray that you need to work with.
//c# controller is compressing the result after the response.binarywrite
[compress]
public ActionResult Print(int id)
{
...
var byteArray=someService.BuildPdf(id);
return return this.PDF(byteArray, "test.pdf");
}
//where PDF is a custom actionresult that eventually does this:
public class PDFResult : ActionResult
{
...
public override void ExecuteResult(ControllerContext context)
{
//Set the HTTP header to excel for download
HttpContext.Current.Response.Clear();
//HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
HttpContext.Current.Response.ContentType = "application/pdf";
HttpContext.Current.Response.AddHeader("content-disposition", string.Concat("attachment; filename=", fileName));
HttpContext.Current.Response.AddHeader("Content-Length", pdfBytes.Length.ToString());
//Write the pdf file as a byte array to the page
HttpContext.Current.Response.BinaryWrite(byteArray);
HttpContext.Current.Response.End();
}
}
//javascript
function pdf(mySearchObject) {
return $http({
method: 'Post',
url: '/api/print/',
data: mySearchObject,
responseType: 'arraybuffer',
headers: {
'Accept': 'application/pdf',
}
}).then(function (response) {
var type = response.headers('Content-Type');
//if response.data is gzipped, this blob will be incorrect. you have to uncompress it first.
var blob = new Blob([response.data], { type: type });
var fileName = response.headers('content-disposition').split('=').pop();
if (window.navigator.msSaveOrOpenBlob) { // for IE and Edge
window.navigator.msSaveBlob(blob, fileName);
} else {
var anchor = angular.element('<a/>');
anchor.css({ display: 'none' }); // Make sure it's not visible
angular.element(document.body).append(anchor); // Attach to document
anchor.attr({
href: URL.createObjectURL(blob),
target: '_blank',
download: fileName
})[0].click();
anchor.remove();
}
});
}
" var blob = new Blob([response.data], { type: type }); "
This will give you that invalid/corrupt file that you are trying to open when you turn that byte array into a file in your javascript if you don't uncompress it first.
To fix this, you have a choice to either prevent gzipping this binary data so that you can properly turn it into the file that you are downloading, or you have to decompress that gzipped data in your javascript code before you turn it into a file.
In addition to Igor's Response.Close(), I would add a Response.Flush().
I also found it necessary to add the following:
Response.Encoding = Encoding.Default
If I didn't include this, my JPEG was corrupt and double the size in bytes.
But only if the handler was returning from an ASPX page. It seemed running from an ASHX this was not required.

Categories