Javascript 如何在ajax中下载每种类型的文件?

Javascript 如何在ajax中下载每种类型的文件?,javascript,c#,download,Javascript,C#,Download,我需要下载每一个js和c类型的文件 这是我的api代码: [HttpPost] public HttpResponseMessage GetFile(DownloadInput input) { HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK); var fileNm = input.FileName; string filePath = (@"C:\Uploads\" +

我需要下载每一个js和c类型的文件

这是我的api代码:

[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;
}
我认为ıam rong这部分代码下载每个文件,但只是work.txt类型的文件,我认为blob类型是错误的,但ıam new这部分代码我尝试了所有代码

这是我的js代码:

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();
}

你可以试试下面的方法,我已经成功地使用过很多次了

首先,您需要确保保存的数据是正确的。因为您需要mimetype来成功下载文件,而不会出现复杂情况

这里是C#(PS:您可以将端点设置为HttpGet,而不是HttpPost)


在javascript中,您可以使用:
window.open('yourApiURL/YourController/FileName')

只是为了验证一下,你是说这只会下载.TXT文件,而且成功了吗?其他类型的错误是什么?错误发生在哪一行?
[HttpGet]
[Route("YourController/{fileName}")]
public HttpResponseMessage Download(string fileName) //Parameter is yours
{
    string filePath = (@"C:\Uploads\" + input.ID + @"\" + fileName);

    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);

    using (var ms = new MemoryStream(bytes))
    {
        var result = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new ByteArrayContent(ms.ToArray())
        };

        result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = fileName //String value of the file name.
        };

        string mimeType = MimeMapping.GetMimeMapping(fileName); //I've found that this does not always work. Go here for a better answer: https://stackoverflow.com/questions/1029740/get-mime-type-from-filename-extension
        result.Content.Headers.ContentType = new MediaTypeHeaderValue(mimeType); //The mime type retrieved from the 

        return result;
    }
}