I wrote a simple asp.net page that simulates file download response. I added the following lines to adjust response:
Response.AddHeader("ETag", "\"" + _EncodedData + "\"");
Response.AddHeader("Last-Modified", lastUpdateTiemStamp);
Response.AddHeader("Accept-Ranges", "bytes");
//Set the ContentType
Response.ContentType = "application/octet-stream";
//Add the file name and attachment,
//which will force the open/cancel/save dialog to show, to the header
Response.AddHeader("Content-Disposition", "attachment;filename=" + dItem.FileName);
//Add the file size into the response header
Response.AddHeader("Content-Length", (endByte - startBytes).ToString());
Response.AddHeader("Connection", "Keep-Alive");
My problem is that Accept-ranges doesn't apear in the response header, but when I change Accept-Ranges to Accept-Ranges1 this header(Accept-Ranges1) will receive a response header.
This problem only occurs when I am debugging the project in Visual Studio. When I am on IIS everything is ok
Related
I'm writing a controller method in ASP.NET WebAPI to download a file. Here's the part where I set the headers and content:
result.Content = new StreamContent(new MemoryStream(csvResult));
result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("text/csv");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = fileName;
result.Content.Headers.ContentLength = csvResult.Length;
The file is being downloaded, but the browser always shows it as "untitled file". Fiddler shows the request coming back with the correct headers:
Content-Disposition: attachment; filename=Report2015-9_createtest3.csv
Content-Length: 1477
Content-Type: text/csv
I've tried it with the content-type as "application/octet-stream" and "text/plain" and those don't work any better either. Any idea what's going on?
Have the following code under IIS/ASP, with Chrome browser under Windows:
Response.ContentType = "application/exe"; //also tried application/octet-stream
Response.AddHeader("content-disposition", "attachment;filename=MyFile.exe");
Response.TransmitFile(Server.MapPath("~/MyFile.exe"));
However when I complete the download, the file I downloaded is a different size to the original (the downloaded file is larger), and the digital sig is missing. How do I fix?
This seems to do the trick:
FileInfo info = new FileInfo(Server.MapPath("~/MyFile.exe"));
Response.ContentType = "application/octet-stream";
Response.AddHeader("content-length", info.Length.ToString());
Response.AddHeader("content-disposition", "attachment;filename=MyFile.exe");
Response.TransmitFile(Server.MapPath("~/MyFile.exe"));
I'm now already trying for hours, but somehow i don't figure out the problem. Here is a sample of my Request:
POST /test/upload/upload.php HTTP/1.0
Host: localhost
User-Agent: TestBrowser
Content-Type: multipart/form-data, boundary=635131229269 //edited
Content-Length: 94
--635131229269
Content-Disposition: form-data; name="testme"
contender
--635131229269--
It is sended over standard TCP/IP Socket to a PHP Server, but the $_POST['testme'] Value is always empty.
Does someone can see the bug in this Request? --> solved
There is a \r\n at the end which doesn't show in the Code here.
Thank you, that solved my first problem.
Maybe you can help my with my second aswell. As i had seen on your profile you are well with C# and there is my second problem. I'm trying to upload a file to my server and the filedata somehow does not requested properly, i think it's because of the encoding, but i'm not sure.
_content = _content
+ "--" + boundary + Environment.NewLine
+ "Content-Disposition: form-data; name=\"" + this._FileVarName + "\"; filename=\"" + Path.GetFileName(this._FilePath) + "\""
+ Environment.NewLine + "Content-Transfer-Encoding: application/octet-stream"
+ Environment.NewLine + Environment.NewLine;
mainContent = this.Combine(Encoding.UTF8.GetBytes(_content), StreamFile(this._FilePath));
private Byte[] StreamFile(string Path)
{
FileStream fs = new FileStream(Path, FileMode.Open, FileAccess.Read);
Byte[] ImageData = new byte[fs.Length];
fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();
return ImageData;
}
You have to specify the boundary (on line 4) without the leading dashes. Also I only count 92 bytes of payload.
Just to make this answer a bit more complete, there has to be a semicolon after "multipart/form-data" (see w3c forms spec)
Content-Type: multipart/form-data; boundary=635131229269
To download mp3 file from 3rd party servers , using general code give
error of need virtual path to download..
using this code :
var fileInfo = new System.IO.FileInfo(filePath);
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", filePath));
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.WriteFile(filePath);
Response.End();
error comes that no virtual path set...i repeat i need to download from 3rd party servers with
full url
How to modify the server values of response header through code behind using asp.net 2.0 with IIS6.0 server.
I have tried Response.Headers.Set("XYZ","ABC");
But it displays throw integrated pipeline error.
Try this:
HttpResponse response = ...;
response.ClearHeaders();
response.ClearContent();
response.ContentType = "application/octet-stream";
response.AppendHeader("Content-Disposition", ""); //(write whatever headers you want like this)
I would use the below in Global.asax.cs to remove the Server header
protected void Application_PreSendRequestHeaders()
{
Response.Headers.Remove("Server");
}