将C#字节数组转换为Javascript Blob,以创建和自动下载PDF文件

将C#字节数组转换为Javascript Blob,以创建和自动下载PDF文件,javascript,c#,pdf,Javascript,C#,Pdf,我在C#中有web api,它返回PDF文件的字节数组。这是C代码 上述文件确实创建并打开了一个pdf文件,但它已损坏 function downloadpdf(pdfUrl) { debugger fetch(pdfUrl).then(resp => resp.arrayBuffer()).then(resp => { const file = new Blob([resp], { type: 'application/pdf' });

我在C#中有web api,它返回PDF文件的字节数组。这是C代码

上述文件确实创建并打开了一个pdf文件,但它已损坏

function downloadpdf(pdfUrl) {
    debugger
    fetch(pdfUrl).then(resp => resp.arrayBuffer()).then(resp => {
        const file = new Blob([resp], { type: 'application/pdf' });

        const fileURL = URL.createObjectURL(file);
        const link = document.createElement('a');
        link.href = fileURL;
        link.download = "FileName" + new Date() + ".pdf";
        link.click();
    });
}

downloadpdf('https://localhost:xxx/api/file/download?fileDocumentId=11')
console.log中('Success:',result),控制台中的输出如下所示:

Success: JVBERi0xLjUNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFuZyhlbi1NWSkgL1N0cnVjdFRyZWVSb290IDI3OCAwIFIvTWFya0luZm88PC9NYXJrZWQgdHJ1ZT4+Pj4NCmVuZG9iag0KMiAwIG9iag0KPDwvVHlwZS9QYWdlcy9Db3VudCAzNS9LaWRzWyAzIDAgUiAxNyAwIFIgMjggMCBSIDMxIDAgUiAzOCAwIFIgNDAgMCBSIDQ0IDAgUiA0NSAwIFIgNDYgMCBSIDQ3IDAgUiA0OCAwIFIgNDkgMCBSIDUxIDAgUiA1MiAwIFIgNTMgMCBSIDU0IDAgUiA1NSAwIFIgNTYgMCBSIDU3IDAgUiA1OCAwIFIgNTkgMCBSIDYwIDAgUiA2MSAwIFIgNjIgMCBSIDYzIDAgUiA2NCAwIFIgNjUgMCBSIDY2IDAgUiA2NyAwIFIgNjggMCBSIDY5IDAgUiA3MCAwIFIgNzEgMCBSIDI2NiAwIFIgMjczIDAgUl0gPj4NCmVuZG9iag0KMyAwIG9iag0KPDwvVHlwZS9QYWdlL1BhcmVud
由于实际输出太长,我在这里截断了它

在谷歌搜索了更多之后,我还尝试了下面的代码,在创建blob之前将字节数组转换为arrayBuffer。它确实创建并下载了PDF,但当我打开PDF时,该PDF也已损坏

function downloadpdf(pdfUrl) {
    debugger
    fetch(pdfUrl).then(resp => resp.arrayBuffer()).then(resp => {
        const file = new Blob([resp], { type: 'application/pdf' });

        const fileURL = URL.createObjectURL(file);
        const link = document.createElement('a');
        link.href = fileURL;
        link.download = "FileName" + new Date() + ".pdf";
        link.click();
    });
}

downloadpdf('https://localhost:xxx/api/file/download?fileDocumentId=11')
对于
FetchFiles
方法,基本上它读取PDF文件并将其转换为字节数组

byte[] fileBytes = await File.ReadAllBytesAsync(file);

您的代码将字节数组返回到JavaScript,您不需要这样做,只需将其作为
FileContentResult
返回,它将向响应写入一个二进制文件,即使您直接在浏览器中调用API,浏览器也会将其作为文件下载

[HttpGet]
[Route("Download")]
public async Task<ActionResult> DownloadFiles(int fileDocumentId)
{
    string filePath = fileDocumentId=11;

    var file = await _fileService.FetchFiles(fileDocumentId);
    string fileName = "FileName" + DateTime.Now.ToString() + ".pdf";

    return File(file.ArchiveData, "application/force-download", fileName);
}
[HttpGet]
[路线(“下载”)]
公共异步任务下载文件(int-fileDocumentId)
{
字符串filePath=fileDocumentId=11;
var file=await\u fileService.FetchFiles(fileDocumentId);
string fileName=“fileName”+DateTime.Now.ToString()+“.pdf”;
返回文件(File.ArchiveData,“应用程序/强制下载”,文件名);
}

我以前也遇到过同样的问题,我记得我通过关闭文件流解决了这个问题,也请共享
FetchFiles
功能content@ElasticCode,我添加了一行
FetchFiles
method的代码,这是否回答了您的问题?
[HttpGet]
[Route("Download")]
public async Task<ActionResult> DownloadFiles(int fileDocumentId)
{
    string filePath = fileDocumentId=11;

    var file = await _fileService.FetchFiles(fileDocumentId);
    string fileName = "FileName" + DateTime.Now.ToString() + ".pdf";

    return File(file.ArchiveData, "application/force-download", fileName);
}