javascript - Safari 10 on Mac not uploading file in XHR -
i'm using xhr upload image external server has cors enabled.
everything works fine in chrome, firefox , ie.
but using safari, server response mime type error. saying file type 'application/octet-stream' while should 'image/*'.
after disabled mime type checking, safari can upload file 0b empty file.
anyone knows why?
var xhr = new xmlhttprequest(); xhr.open('post', 'http://up-z1.qiniu.com/', true); var formdata; formdata = new formdata(); formdata.append('key', file.name); formdata.append('token', acesstoken); formdata.append('file', file); xhr.onreadystatechange = function (response) { if (xhr.readystate == 4 && xhr.status == 200 && xhr.responsetext != "") { callback(true,null); } else if (xhr.status != 200 && xhr.responsetext) { callback(false,null); } }; xhr.send(formdata);
so according stackoverflow question, cant remember 1 safari has bug when other browsers file.toblob(), safari file.tostring(). workaround write own file blob function , upload blob.
var xhr = new xmlhttprequest(); xhr.open('post', 'http://up-z1.qiniu.com/', true); var formdata; formdata = new formdata(); formdata.append('key', file.name); formdata.append('token', acesstoken); formdata.append('file', filetoblob(file)); xhr.onreadystatechange = function (response) { if (xhr.readystate == 4 && xhr.status == 200 && xhr.responsetext != "") { callback(true,null); } else if (xhr.status != 200 && xhr.responsetext) { callback(false,null); } }; xhr.send(formdata);
Comments
Post a Comment