简体   繁体   English

使用jQuery作为Relay上传到Image Server

[英]Upload to Image Server using jQuery as Relay

Problem: 问题:

I have a situation where I'd like to upload a file (pdf, image, etc.) to an API Endpoint that accepts one of these types of files. 我有一种情况,我想将文件(pdf,图像等)上传到接受这些类型文件之一的API端点。 However, the file is located on another web service somewhere. 但是,该文件位于某个地方的另一个Web服务上。 I'm trying to devise a clever solution that will allow me to (a) download the remote file (and store it as bytes in memory or something) then (b) upload that file through the API. 我正在尝试设计一个聪明的解决方案,允许我(a)下载远程文件(并将其作为字节存储在内存或其他内容中),然后(b)通过API上传该文件。

I have jQuery code that demonstrates how to upload a local file using jQuery with no backend code, but I'd like to extend it to allow me to upload something that is stored remotely. 我有jQuery代码演示如何使用没有后端代码的jQuery上传本地文件,但我想扩展它以允许我上传远程存储的东西。

Constraints: 约束:

  • I don't want to use any backend infrastructure on my image uploading page (ie. no php, python, ruby, etc.) 我不想在我的图片上传页面上使用任何后端基础设施(即没有php,python,ruby等)
  • I don't want the end user of my form to need to download the file to their machine and upload the file as a two-step process. 我不希望表单的最终用户需要将文件下载到他们的计算机上,并将文件作为两个步骤上传。

What I've got so far: 到目前为止我得到了什么:

I've seen some solutions on SO that kind-of connect the dots here in terms of downloading a file as a bytearray, but nothing that demonstrates how you might upload that. 我已经在SO上看到了一些解决这些问题的解决方案,就像下载一个文件作为bytearray一样,但没有任何东西可以证明你如何上传它。

Keep in mind, Stripe is the example I have, but I'd like to try and replicate this on say Imgur or another API (if I can get this working). 请记住,Stripe就是我的例子,但是我想尝试在Imgur或其他API上复制它(如果我可以使用它)。 Hopefully someone else has some ideas! 希望别人有一些想法!

  $('#fileinfo').submit(function(event) {
    event.preventDefault();

    var data = new FormData();

    var publishableKey = 'pk_test_***';

    data.append('file', $('#file-box')[0].files[0]);
    data.append('purpose', 'identity_document');

    $.ajax({
      url: 'https://uploads.stripe.com/v1/files',
      data: data,
      headers: {
        'Authorization': 'Bearer ' + publishableKey,
        // 'Stripe-Account': 'acct_STRIPE-ACCOUNT-ID'
      },
      cache: false,
      contentType: false,
      processData: false,
      type: 'POST',
    }).done(function(data) {
      $('#label-results').text('Success!');
      $('#upload-results').text(JSON.stringify(data, null, 3));
    }).fail(function(response, type, message) {
      $('#label-results').text('Failure: ' + type + ', ' + message);
      $('#upload-results').text(JSON.stringify(response.responseJSON, null, 3));
    });

    return false;
  });

I actually got this working for Stripe by doing this: 我实际上通过这样做得到了Stripe的工作:

https://jsfiddle.net/andrewnelder/up59zght/ https://jsfiddle.net/andrewnelder/up59zght/

var publishableKey = "pk_test_xxx";  // Platform Publishable Key
var stripeAccount = "acct_xxx";  // Connected Account ID

$(document).ready(function () {
    $('#file-upload').on('submit', function (e) {
    e.preventDefault();

    console.log('Clicked!');

    var route = $('#file-route').val();  // URL OF FILE
    var fname = route.split("/").slice(-1)[0].split("?")[0];

    var blob = fetchBlob(route, fname, uploadBlob);

  });
});

function fetchBlob(route, fname, uploadBlob) {

  console.log('Fetching...')

  var oReq = new XMLHttpRequest();
  oReq.open("GET", route, true);
  oReq.responseType = "blob";
  oReq.onload = function(e) {
    var blob = oReq.response;
    console.log('Fetched!')
    uploadBlob(fname, blob);
  };
  oReq.send();

}

function uploadBlob(fname, blob) {

  var fd = new FormData();
  fd.append('file', blob);
  fd.append('purpose', 'identity_document');

    console.log('Uploading...');

    $.ajax({
    url: 'https://uploads.stripe.com/v1/files',
    data: fd,
    headers: {
      'Authorization': 'Bearer ' + publishableKey,
      'Stripe-Account': stripeAccount
    },
    cache: false,
    contentType: false,
    processData: false,
    type: 'POST',
  }).done(function(data) {
    console.log('Uploaded!')
  }).fail(function(response, type, message) {
    console.log(message);
  });

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM