从通过AJAX另一台服务器上传图像使用JavaScript [英] Upload image with JavaScript from another server via AJAX
问题描述
我想(如果可能的话),以通过JavaScript存放在其他网站上(而不是计算机)(jQuery的允许),一个AJAX请求的图像的上传。
I'm trying (if it's possible) to make an upload of an image stored on another site (not on computer) via JavaScript (jQuery allowed) with an AJAX request.
比方说,我们有像 http://example.com/a.jpg
。我需要一个AJAX请求,submiting此图片 http://test.com/process.php
。
Lets say that we have the image http://example.com/a.jpg
. I need to make an AJAX request, submiting this image to http://test.com/process.php
.
- 在我不能编辑文件
process.php
来接受任何不是的有效的上传文件的 - 在浏览器的支持并不重要。
- I can't edit the file
process.php
to accept anything than a valid uploaded file. - Browser support is not important.
这甚至可能吗?安全问题,因为我们canţtdynamicaly填充文件字段,所以也许是另一种方式来发送文件,而无需用户选择该文件。
Is this even possible ? Because of security issues we canțt dynamicaly populate a file field, so maybe the is another way to send the file without having the user to select the file.
我想我应该用 FORMDATA
,不知道。
I think I should use FormData
, not sure.
推荐答案
// Recieving文件。
// Recieving File.
var url = "http://example.com/a.jpg"
var oReq = new XMLHttpRequest();
oReq.onload = function(e) {
var blobData = oReq.response; // not responseText
// Sending data.
var formData = new FormData();
// JavaScript file-like object
var blob = new Blob([blobData], { type: "image/jpeg"});
formData.append("webmasterfile", blob);
var request = new XMLHttpRequest();
request.open("POST", "http://test.com/process.php");
request.send(formData);
}
oReq.open("GET", url, true);
oReq.responseType = "blob";
oReq.send();
资源:
https://developer.mozilla.org/en-美国/文档/网页/ API / FORMDATA /附加
的https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects
的https://developer.mozilla.org/en-US/docs/Web/API/XMLHtt$p$pquest/Using_XMLHtt$p$pquest
这篇关于从通过AJAX另一台服务器上传图像使用JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!