Upload File With Ajax XmlHttpRequest
Hi i am trying to send file with xmlhttprequest with this code.
<script>
var url= "http://localhost:80/....";
$(document).ready(function(){
document.getElementById('upload').addEventListener('change', function(e) {
var file = this.files[0];
var xhr = new XMLHttpRequest();
xhr.file = file; // not necessary if you create scopes like this
xhr.addEventListener('progress', function(e) {
var done = e.position || e.loaded, total = e.totalSize || e.total;
console.log('xhr progress: ' + (Math.floor(done/total*1000)/10) + '%');
}, false);
if ( xhr.upload ) {
xhr.upload.onprogress = function(e) {
var done = e.position || e.loaded, total = e.totalSize || e.total;
console.log('xhr.upload progress: ' + done + ' / ' + total + ' = ' + (Math.floor(done/total*1000)/10) + '%');
};
}
xhr.onreadystatechange = function(e) {
if ( 4 == this.readyState ) {
console.log(['xhr upload complete', e]);
}
};
xhr.open('post', url, true);
xhr.setRequestHeader("Content-Type","multipart/form-data");
xhr.send(file);
}, false);
});
</script>
but i got this error : the request was rejected because no multipart boundary was found
help me pls..
javascript ajax jquery file-upload xmlhttprequest
add a comment |
Hi i am trying to send file with xmlhttprequest with this code.
<script>
var url= "http://localhost:80/....";
$(document).ready(function(){
document.getElementById('upload').addEventListener('change', function(e) {
var file = this.files[0];
var xhr = new XMLHttpRequest();
xhr.file = file; // not necessary if you create scopes like this
xhr.addEventListener('progress', function(e) {
var done = e.position || e.loaded, total = e.totalSize || e.total;
console.log('xhr progress: ' + (Math.floor(done/total*1000)/10) + '%');
}, false);
if ( xhr.upload ) {
xhr.upload.onprogress = function(e) {
var done = e.position || e.loaded, total = e.totalSize || e.total;
console.log('xhr.upload progress: ' + done + ' / ' + total + ' = ' + (Math.floor(done/total*1000)/10) + '%');
};
}
xhr.onreadystatechange = function(e) {
if ( 4 == this.readyState ) {
console.log(['xhr upload complete', e]);
}
};
xhr.open('post', url, true);
xhr.setRequestHeader("Content-Type","multipart/form-data");
xhr.send(file);
}, false);
});
</script>
but i got this error : the request was rejected because no multipart boundary was found
help me pls..
javascript ajax jquery file-upload xmlhttprequest
add a comment |
Hi i am trying to send file with xmlhttprequest with this code.
<script>
var url= "http://localhost:80/....";
$(document).ready(function(){
document.getElementById('upload').addEventListener('change', function(e) {
var file = this.files[0];
var xhr = new XMLHttpRequest();
xhr.file = file; // not necessary if you create scopes like this
xhr.addEventListener('progress', function(e) {
var done = e.position || e.loaded, total = e.totalSize || e.total;
console.log('xhr progress: ' + (Math.floor(done/total*1000)/10) + '%');
}, false);
if ( xhr.upload ) {
xhr.upload.onprogress = function(e) {
var done = e.position || e.loaded, total = e.totalSize || e.total;
console.log('xhr.upload progress: ' + done + ' / ' + total + ' = ' + (Math.floor(done/total*1000)/10) + '%');
};
}
xhr.onreadystatechange = function(e) {
if ( 4 == this.readyState ) {
console.log(['xhr upload complete', e]);
}
};
xhr.open('post', url, true);
xhr.setRequestHeader("Content-Type","multipart/form-data");
xhr.send(file);
}, false);
});
</script>
but i got this error : the request was rejected because no multipart boundary was found
help me pls..
javascript ajax jquery file-upload xmlhttprequest
Hi i am trying to send file with xmlhttprequest with this code.
<script>
var url= "http://localhost:80/....";
$(document).ready(function(){
document.getElementById('upload').addEventListener('change', function(e) {
var file = this.files[0];
var xhr = new XMLHttpRequest();
xhr.file = file; // not necessary if you create scopes like this
xhr.addEventListener('progress', function(e) {
var done = e.position || e.loaded, total = e.totalSize || e.total;
console.log('xhr progress: ' + (Math.floor(done/total*1000)/10) + '%');
}, false);
if ( xhr.upload ) {
xhr.upload.onprogress = function(e) {
var done = e.position || e.loaded, total = e.totalSize || e.total;
console.log('xhr.upload progress: ' + done + ' / ' + total + ' = ' + (Math.floor(done/total*1000)/10) + '%');
};
}
xhr.onreadystatechange = function(e) {
if ( 4 == this.readyState ) {
console.log(['xhr upload complete', e]);
}
};
xhr.open('post', url, true);
xhr.setRequestHeader("Content-Type","multipart/form-data");
xhr.send(file);
}, false);
});
</script>
but i got this error : the request was rejected because no multipart boundary was found
help me pls..
javascript ajax jquery file-upload xmlhttprequest
javascript ajax jquery file-upload xmlhttprequest
edited May 20 '13 at 2:58
user1823761
asked Jun 2 '11 at 6:19
Sedat BaşarSedat Başar
2,30121828
2,30121828
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
- There is no such thing as
xhr.file = file;
; the file object is not supposed to be attached this way.
xhr.send(file)
doesn't send the file. You have to use theFormData
object to wrap the file into amultipart/form-data
post data object:
var formData = new FormData();
formData.append("thefile", file);
xhr.send(formData);
After that, the file can be access in $_FILES['thefile']
(if you are using PHP).
Remember, MDC and Mozilla Hack demos are your best friends.
EDIT: The (2) above was incorrect. It does send the file, but it would send it as raw post data. That means you would have to parse it yourself on the server (and it's often not possible, depend on server configuration). Read how to get raw post data in PHP here.
1
ty so much that helped me..
– Sedat Başar
Jun 2 '11 at 11:48
5
xhr.send(file) is based on XHR2 which is a new version of the XMLHttpRequest object only avalable in some browsers.
– nkassis
Apr 20 '12 at 19:19
1
@nkassis yeah, but it would send the file itself as the post body, instead of constructing a multipart postdata for server to parse.
– timdream
Apr 22 '12 at 18:19
How come jquery can do it and pure javascript cannot?
– John Smith
Oct 18 '12 at 22:48
2
This method requires at least IE10 or Android 3.0.
– andreszs
Aug 5 '14 at 15:14
|
show 4 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6211145%2fupload-file-with-ajax-xmlhttprequest%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
- There is no such thing as
xhr.file = file;
; the file object is not supposed to be attached this way.
xhr.send(file)
doesn't send the file. You have to use theFormData
object to wrap the file into amultipart/form-data
post data object:
var formData = new FormData();
formData.append("thefile", file);
xhr.send(formData);
After that, the file can be access in $_FILES['thefile']
(if you are using PHP).
Remember, MDC and Mozilla Hack demos are your best friends.
EDIT: The (2) above was incorrect. It does send the file, but it would send it as raw post data. That means you would have to parse it yourself on the server (and it's often not possible, depend on server configuration). Read how to get raw post data in PHP here.
1
ty so much that helped me..
– Sedat Başar
Jun 2 '11 at 11:48
5
xhr.send(file) is based on XHR2 which is a new version of the XMLHttpRequest object only avalable in some browsers.
– nkassis
Apr 20 '12 at 19:19
1
@nkassis yeah, but it would send the file itself as the post body, instead of constructing a multipart postdata for server to parse.
– timdream
Apr 22 '12 at 18:19
How come jquery can do it and pure javascript cannot?
– John Smith
Oct 18 '12 at 22:48
2
This method requires at least IE10 or Android 3.0.
– andreszs
Aug 5 '14 at 15:14
|
show 4 more comments
- There is no such thing as
xhr.file = file;
; the file object is not supposed to be attached this way.
xhr.send(file)
doesn't send the file. You have to use theFormData
object to wrap the file into amultipart/form-data
post data object:
var formData = new FormData();
formData.append("thefile", file);
xhr.send(formData);
After that, the file can be access in $_FILES['thefile']
(if you are using PHP).
Remember, MDC and Mozilla Hack demos are your best friends.
EDIT: The (2) above was incorrect. It does send the file, but it would send it as raw post data. That means you would have to parse it yourself on the server (and it's often not possible, depend on server configuration). Read how to get raw post data in PHP here.
1
ty so much that helped me..
– Sedat Başar
Jun 2 '11 at 11:48
5
xhr.send(file) is based on XHR2 which is a new version of the XMLHttpRequest object only avalable in some browsers.
– nkassis
Apr 20 '12 at 19:19
1
@nkassis yeah, but it would send the file itself as the post body, instead of constructing a multipart postdata for server to parse.
– timdream
Apr 22 '12 at 18:19
How come jquery can do it and pure javascript cannot?
– John Smith
Oct 18 '12 at 22:48
2
This method requires at least IE10 or Android 3.0.
– andreszs
Aug 5 '14 at 15:14
|
show 4 more comments
- There is no such thing as
xhr.file = file;
; the file object is not supposed to be attached this way.
xhr.send(file)
doesn't send the file. You have to use theFormData
object to wrap the file into amultipart/form-data
post data object:
var formData = new FormData();
formData.append("thefile", file);
xhr.send(formData);
After that, the file can be access in $_FILES['thefile']
(if you are using PHP).
Remember, MDC and Mozilla Hack demos are your best friends.
EDIT: The (2) above was incorrect. It does send the file, but it would send it as raw post data. That means you would have to parse it yourself on the server (and it's often not possible, depend on server configuration). Read how to get raw post data in PHP here.
- There is no such thing as
xhr.file = file;
; the file object is not supposed to be attached this way.
xhr.send(file)
doesn't send the file. You have to use theFormData
object to wrap the file into amultipart/form-data
post data object:
var formData = new FormData();
formData.append("thefile", file);
xhr.send(formData);
After that, the file can be access in $_FILES['thefile']
(if you are using PHP).
Remember, MDC and Mozilla Hack demos are your best friends.
EDIT: The (2) above was incorrect. It does send the file, but it would send it as raw post data. That means you would have to parse it yourself on the server (and it's often not possible, depend on server configuration). Read how to get raw post data in PHP here.
edited May 20 '13 at 2:43
answered Jun 2 '11 at 9:09
timdreamtimdream
4,30831722
4,30831722
1
ty so much that helped me..
– Sedat Başar
Jun 2 '11 at 11:48
5
xhr.send(file) is based on XHR2 which is a new version of the XMLHttpRequest object only avalable in some browsers.
– nkassis
Apr 20 '12 at 19:19
1
@nkassis yeah, but it would send the file itself as the post body, instead of constructing a multipart postdata for server to parse.
– timdream
Apr 22 '12 at 18:19
How come jquery can do it and pure javascript cannot?
– John Smith
Oct 18 '12 at 22:48
2
This method requires at least IE10 or Android 3.0.
– andreszs
Aug 5 '14 at 15:14
|
show 4 more comments
1
ty so much that helped me..
– Sedat Başar
Jun 2 '11 at 11:48
5
xhr.send(file) is based on XHR2 which is a new version of the XMLHttpRequest object only avalable in some browsers.
– nkassis
Apr 20 '12 at 19:19
1
@nkassis yeah, but it would send the file itself as the post body, instead of constructing a multipart postdata for server to parse.
– timdream
Apr 22 '12 at 18:19
How come jquery can do it and pure javascript cannot?
– John Smith
Oct 18 '12 at 22:48
2
This method requires at least IE10 or Android 3.0.
– andreszs
Aug 5 '14 at 15:14
1
1
ty so much that helped me..
– Sedat Başar
Jun 2 '11 at 11:48
ty so much that helped me..
– Sedat Başar
Jun 2 '11 at 11:48
5
5
xhr.send(file) is based on XHR2 which is a new version of the XMLHttpRequest object only avalable in some browsers.
– nkassis
Apr 20 '12 at 19:19
xhr.send(file) is based on XHR2 which is a new version of the XMLHttpRequest object only avalable in some browsers.
– nkassis
Apr 20 '12 at 19:19
1
1
@nkassis yeah, but it would send the file itself as the post body, instead of constructing a multipart postdata for server to parse.
– timdream
Apr 22 '12 at 18:19
@nkassis yeah, but it would send the file itself as the post body, instead of constructing a multipart postdata for server to parse.
– timdream
Apr 22 '12 at 18:19
How come jquery can do it and pure javascript cannot?
– John Smith
Oct 18 '12 at 22:48
How come jquery can do it and pure javascript cannot?
– John Smith
Oct 18 '12 at 22:48
2
2
This method requires at least IE10 or Android 3.0.
– andreszs
Aug 5 '14 at 15:14
This method requires at least IE10 or Android 3.0.
– andreszs
Aug 5 '14 at 15:14
|
show 4 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6211145%2fupload-file-with-ajax-xmlhttprequest%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown