admin管理员组

文章数量:1287517

This is my little javascript code:

<script>

var formData = new FormData();

URL = "view.php?fetchImageById=1";

formData.append("imageFile", ....);

formData.append("author","user");
formData.append("description","image");

x=new XMLHttpRequest();
x.open("POST","upload.php",true);
x.setRequestHeader("Content-type", "multipart/form-data");
x.setRequestHeader("Content-Length",formData.length);
x.send(formData);
</script>

I don't know how to append the URL to the formData.

This is my little javascript code:

<script>

var formData = new FormData();

URL = "view.php?fetchImageById=1";

formData.append("imageFile", ....);

formData.append("author","user");
formData.append("description","image");

x=new XMLHttpRequest();
x.open("POST","upload.php",true);
x.setRequestHeader("Content-type", "multipart/form-data");
x.setRequestHeader("Content-Length",formData.length);
x.send(formData);
</script>

I don't know how to append the URL to the formData.

Share Improve this question asked May 15, 2016 at 18:08 Diego LopezDiego Lopez 1711 gold badge1 silver badge7 bronze badges 1
  • The same way you append everything else… (although I suspect that you don't actually mean "append the URL to the formData"). – Quentin Commented May 15, 2016 at 18:10
Add a ment  | 

1 Answer 1

Reset to default 9

You could perform two XMLHttpRequest()s; first GET request image as a Blob first by setting responseType to "blob"; then append Blob response to FormData at POST

var formData = new FormData();   
URL = "view.php?fetchImageById=1";    
var x;

var request = new XMLHttpRequest();
request.responseType = "blob";
request.onload = function() {
  formData.append("imageFile", request.response);
  formData.append("author","user");
  formData.append("description","image");
  x = new XMLHttpRequest();
  x.open("POST","upload.php",true);
  x.setRequestHeader("Content-type", "multipart/form-data");
  x.setRequestHeader("Content-Length", formData.length);
  x.send(formData);
}
request.open("GET", URL);
request.send();

本文标签: form dataHow to append an image from URL to a FormDataJavascriptStack Overflow