admin管理员组文章数量:1386673
I am trying to upload an image to server using using XMLHttpRequest but fails. Below is the code I am using.
<input type="submit" onclick="fn()" value="Click"/>
<script type="text/javascript">
function fn(){
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
}
catch (e) {
console.log("Not firefox");
}
xmlhttp = new XMLHttpRequest();
var requestUrl = "http://localhost:9000/laptop.png";
xmlhttp.open("GET",requestUrl,true);
xmlhttp.overrideMimeType("text/plain; charset=x-user-defined");
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status == 200) {
imageDataPost(xmlhttp.responseText);
console.log(xmlhttp.responseText);
}
}
}
xmlhttp.send();
}
function imageDataPost(imgData) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
}
catch (e) {
console.log("Not firefox");
}
xmlhttp = new XMLHttpRequest();
var requestUrl = "http://server_url/fileupload/";
xmlhttp.open("POST",requestUrl,true);
xmlhttp.overrideMimeType("text/plain; charset=x-user-defined");
xmlhttp.setRequestHeader("Content-type", "multipart/form-data");
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status == 200) {
alert("success");
console.log(xmlhttp.responseText);
}
else {
alert("Failed");
}
}
}
xmlhttp.send("upload="+imgData);
}
Any Idea whats wrong here. I am getting (an empty string) as response.. File is not uploaded to server. Guys please help.
I am trying to upload an image to server using using XMLHttpRequest but fails. Below is the code I am using.
<input type="submit" onclick="fn()" value="Click"/>
<script type="text/javascript">
function fn(){
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
}
catch (e) {
console.log("Not firefox");
}
xmlhttp = new XMLHttpRequest();
var requestUrl = "http://localhost:9000/laptop.png";
xmlhttp.open("GET",requestUrl,true);
xmlhttp.overrideMimeType("text/plain; charset=x-user-defined");
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status == 200) {
imageDataPost(xmlhttp.responseText);
console.log(xmlhttp.responseText);
}
}
}
xmlhttp.send();
}
function imageDataPost(imgData) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
}
catch (e) {
console.log("Not firefox");
}
xmlhttp = new XMLHttpRequest();
var requestUrl = "http://server_url/fileupload/";
xmlhttp.open("POST",requestUrl,true);
xmlhttp.overrideMimeType("text/plain; charset=x-user-defined");
xmlhttp.setRequestHeader("Content-type", "multipart/form-data");
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status == 200) {
alert("success");
console.log(xmlhttp.responseText);
}
else {
alert("Failed");
}
}
}
xmlhttp.send("upload="+imgData);
}
Any Idea whats wrong here. I am getting (an empty string) as response.. File is not uploaded to server. Guys please help.
Share Improve this question edited Jun 10, 2011 at 9:56 Amal Kumar S asked Jun 10, 2011 at 9:49 Amal Kumar SAmal Kumar S 16.1k19 gold badges58 silver badges89 bronze badges 2- What webserver do you use? When you get an error from the server, you should first take a look at the server, and not the client part. Do you have anything in the error logs on the server side? – fab Commented Jun 10, 2011 at 10:00
- its the xmlhttp.responseText its some binary values – Amal Kumar S Commented Jun 10, 2011 at 10:01
2 Answers
Reset to default 2You simply can't upload a file with pure Javascript (at least not in a cross browser way, see this article for more information)
This is because XMLHttpRequest has no support for multipart/form-data, you can do tricks like using an iframe or use flash.
There are enough articles on the internet that explain this.
- http://www.ajaxf1./tutorial/ajax-file-upload-tutorial.html
- http://www.faqs/rfcs/rfc2388.html
Your code looks fine. The reason you are not able to upload file may be since you are accessing the server through the localhost and XMLHttpRequest does not work on localhost. It gives an error "No 'Access-Control-Allow-Origin' header is present on the requested resource" whenever you try to upload a file using XMLHttpRequest to the localhost, what you need to do is access the server using a domain name or through the IP Address
You can find a working example here. The link also discusses the above mentioned problem in the Note section.
You can also find a description of above problem at the link.
本文标签: javascriptUnable to upload image to server using XMLHttpRequestStack Overflow
版权声明:本文标题:javascript - Unable to upload image to server using XMLHttpRequest - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744567986a2613167.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论