【问题标题】:File API - works with cURL but fails with AJAX and Postman文件 API - 适用于 cURL,但不适用于 AJAX 和 Postman
【发布时间】:2018-11-22 08:55:59
【问题描述】:

我正在运行 gotenberg 作为我的文档到 PDF 转换 API 并且它正在使用 cURL

cURL 命令如下所示

curl --request POST --url http://example.com  --header 'Content-Type: multipart/form-data' --form files=@file.docx > result.pdf

API 仅适用于 cURL,当我尝试使用 PostmanAJAX 击中相同的 API 时,我会收到回复,但要保存详细信息或使用 Postman 预览回复得到一个空的 PDF 文件。

我的AJAX 请求如下所示

  var settings = {
        "async": true,
        "crossDomain": true,
        "url": "http://convertionapi.com",
        "method": "POST",
        "processData": false,
        "contentType": false,
        "data": form,
        success: function(data){
            console.log("Success", data);
            s3.upload({
                Key: "files/test.pdf",
                Body: data,
                ContentType: 'application/pdf',
                ACL: 'public-read',
            },function(err, data) {...}
    },
    error: function(err) { 
        console.log("Error", err);
    }
}

谁能说明我的请求发生了什么?

我的响应得到以下标题,但创建的文件是空的

【问题讨论】:

  • 你在console.log中得到了什么,当你写“成功”+数据时?
  • 我越来越喜欢imgur.com/a/hfPQdWB
  • 然后,您似乎从 ajax 请求中正确获取了 PDF。现在你需要用这些信息做什么?下载吗?
  • 我正在尝试将 PDF 保存到 S3,并且正在 S3 上创建 PDF,但整个 PDF 只是一个空文件,空文件是指没有内容,只是普通的 A4 纸: /
  • 我不知道 S3 是什么。以防万一,你能看到在 S3 中创建的文件的大小吗?是 51563 字节吗?还是0bytes?

标签: jquery ajax curl


【解决方案1】:

我需要处理blob 响应,因此我的AJAX 呼叫必须看起来像这样。

var settings = {
        "async": true,
        "crossDomain": true,
        "url": "http://convertionapi.com",
        "method": "POST",
        "processData": false,
        "contentType": false,
        "data": form,
        "xhr": function(){
               var xhr = new XMLHttpRequest();
               xhr.responseType= 'blob'
               return xhr;
        },
        success: function(data){
            console.log("Success", data);
            s3.upload({
                Key: "files/test.pdf",
                Body: data,
                ContentType: 'application/pdf',
                ACL: 'public-read',
            },function(err, data) {...}
    },
    error: function(err) { 
        console.log("Error", err);
    }
}

【讨论】:

    猜你喜欢
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    相关资源
    最近更新 更多