I need to download every file of type js and c#.
This is my api code:
[HttpPost]
public HttpResponseMessage GetFile(DownloadInput input)
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
var fileNm = input.FileName;
string filePath = (@"C:\Uploads\" + input.ID + @"\" + fileNm);
if (!File.Exists(filePath))
{
response.StatusCode = HttpStatusCode.NotFound;
response.ReasonPhrase = string.Format("File not found: {0} .", fileNm);
throw new HttpResponseException(response);
}
byte[] bytes = File.ReadAllBytes(filePath);
response.Content = new ByteArrayContent(bytes);
response.Content.Headers.ContentLength = bytes.LongLength;
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = fileNm;
response.Content.Headers.ContentType = new MediaTypeHeaderValue(MimeMapping.GetMimeMapping(filePath));
return response;
}
I think ı am rong this part this code download every file but just work .txt type file, I think blob type is false but ı am new this subject ı am tried every code ,
This is my js code:
function FileDown(response, name) {
let blob = new Blob([response], { type: "application/octet-stream" });
let link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = name;
link.click();
}