【问题标题】:Uploading file from heroku to external service with trigger.io/forge使用 trigger.io/forge 将文件从 heroku 上传到外部服务
【发布时间】:2014-11-04 07:52:59
【问题描述】:

我正在尝试从部署到 Heroku 的 trigger.io 应用将文件上传到外部服务。

但似乎不可能创建我们在 Android 和 iPhone 上使用的上传文件的请求。

有效的请求如下所示:

Remote Address:xx.xx.xx.xx:80
Request URL:http://xxx.xx/umbraco/api/fileupload/post
Request Method:POST
Status Code:200 OK
Request Headersview parsed
POST /umbraco/api/fileupload/post HTTP/1.1
Host: xxx.xx
Connection: keep-alive
Content-Length: 194705
Pragma: no-cache
Cache-Control: no-cache
Origin: chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary9xc19ClYRhe1JArC
Accept: */*
Accept-Encoding: gzip,deflate
Accept-Language: en-US,en;q=0.8
Cookie: UMB_PANEL=821x877; ASP.NET_SessionId=ae54dmv0uyhzch450xvzpe1a; yourAuthCookie=E84CAA1888280530AB37DCDC59FC7CC90686BB3445C4BA0EEC0F11A13C7CD474A6D33C5AF7
Request Payload
------WebKitFormBoundary9xc19ClYRhe1JArC
Content-Disposition: form-data; name="myFile"; filename="swimming-elephant1.jpg"
Content-Type: image/jpeg


------WebKitFormBoundary9xc19ClYRhe1JArC--
Response Headersview source
Cache-Control:no-cache
Content-Length:38
Content-Type:application/json; charset=utf-8
Date:Mon, 03 Nov 2014 12:02:39 GMT
Expires:-1
Pragma:no-cache
X-Robots-Tag:noindex, nofollow, noarchive, nosnippet

除非使用 multipart/form-data 发送,否则上传将失败,但无论我使用什么 Content-Type,forge 始终使用 x-www-form-urlencoded。

这是我上传的sn-p:

var selected_file = document.getElementById('webImg').files[0];
forge.logging.info('triggered get file:' + selected_file);
$ionicLoading.show({duration:1500});
var apiCall = globalApi + 'fileupload/post';
forge.request.ajax({
  type: 'POST',
  cache: false, 
  contentType: 'multipart/form-data',
  url: apiCall,
  data: [selected_file],
  success: function(data) {
    alert('Uploaded file as '+ eval(unescape(data)).toString());
    $ionicLoading.hide();
    $scope.news.ImageUrl = eval(unescape(data)).toString();
    $scope.$apply();
  },
  error: function(error) {
    forge.logging.info('Failed to upload file: '+error.message);
    NewsFactory.checkResponse(error,$scope);
  }
});

是否可以将 multipart/form-data 用于 forge.request.ajax 请求?

【问题讨论】:

    标签: node.js heroku desktop-application trigger.io


    【解决方案1】:

    如果您尝试以下操作会发生什么:

    forge.request.ajax({
        url: some_url,
        files: [file1, file2],
        fileUploadMethod: "multipart",
        success: function (data) {
        },
        progress: function (progress) {
            forge.logging.log("Progress: "+Math.round(100*progress.done/progress.total)+"%");
        }
    });
    

    其中file1, file2 是通过forge.file API 获得的文件对象:https://trigger.io/modules/file/current/docs/index.html

    【讨论】:

    • 从 iOS 和 Android 上传工作正常,我已经这样做了:forge.request.ajax({ type: 'POST', url: apiCall, files: [file], success: function(data) { alert('Uploaded file as '+ eval(unescape(data)).toString()); $scope.news.ImageUrl = eval(unescape(data)).toString(); $scope.$apply(); }, error: function(error) { forge.logging.info('Failed to upload file: '+error.message); NewsFactory.checkResponse(error,$scope); } });
    • 但由于我在网络平台 (Heroku) 上,据我所知,我没有可用的 forge.file?
    猜你喜欢
    • 1970-01-01
    • 2012-05-16
    • 2011-10-21
    • 1970-01-01
    • 2019-07-12
    • 2021-12-03
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    相关资源
    最近更新 更多