Prevent IDM from downloading automatically in web api
Change the mime type to application/octet-stream
as a way to work around your problem. Make sure that the file name includes a proper file extension so that it can be recognized by the client system once downloaded.
Another issue is the attachment
disposition of the content which typically forces it to save it as a file download. Change it to inline
so that the client can consume it without IDM trying to download it as an attachment.
var stream = new FileStream(path, FileMode.Open, FileAccess.Read);var content new StreamContent(stream);content.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline");content.Headers.ContentDisposition.FileName = fileName;content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");var response = Request.CreateResponse(HttpStatusCode.OK);response.Content = content;return response;
I have try to use HttpResponseMessage
.
If I use ContentDisposition
is inline
then response break the file. If use attachment
then IDM can detect it.
At the end of the day, I found Accept-Ranges
header can make download without IDM but it not valid in HttpResponseMessage
.
You can try out my code below to make download file without IDM:
[HttpGet][Route("~/download/{filename}")]public void Download(string filename){ // TODO lookup file path by {filename} // If you want to have "." in {filename} you need enable in webconfig string filePath = "<path>"; // your file path here byte[] fileBytes = File.ReadAllBytes(filePath); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("Accept-Ranges", "bytes"); HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.AddHeader("ContentDisposition", "attachment, filename=" + filename); HttpContext.Current.Response.BinaryWrite(fileBytes); HttpContext.Current.Response.End();}
Note: filename
parameter serve for download file name so you can config in webconfig
if you want to have file extension (disabled by default).