我找到了一些在 Bash 中使用 cURL 从官方客户端外部上传的答案,但我很难使用 XMLHttpRequest 将其转换为 JavaScript。主要问题是我认为 Puush 不支持 CORS。这是迄今为止我的相关代码:
var xml = new XMLHttpRequest(),
fd = new FormData();
xml.open("POST", "http://puush.me/api/up", true);
xml.withCredentials = true; //I've tried with and without this line, neither seems to work
//the following variables are specified from cURL demos I found
fd.append("z", "poop"); //I honestly don't get this part
fd.append("e", "username@domain.com");
fd.append("k", apiKey); //which I copied from my account settings on Puush
fd.append("f", input.files[0]); //where "input" is <input type="file"/>
//add event handlers
xml.addEventListener("progress", progress);
xml.addEventListener("load", loaded);
xml.addEventListener("error", error);
xml.addEventListener("abort", aborted);
xml.send(fd); //send the FormData as a "multipart/form-data" request
无论我尝试什么,我总是得到相同的响应,并且错误处理程序总是被调用,并且控制台中的错误日志表明服务器没有响应 Access-Control-Allow-Origin: http://localhost
(是的,我不只是简单地使用 file://协议(protocol),因为空源也被拒绝)。
编辑:原始 bash 代码可以在以下链接找到:
http://pastebin.com/ZnbY91EA
https://github.com/nekodex/lazymode/blob/master/puush/puu.sh
https://github.com/blha303/puush-linux/blob/master/puush
https://github.com/NickHu/puush-linux/blob/master/puush
最佳答案
事实证明,请求实际上已完全通过,但由于 CORS 的性质,响应被拒绝。因此文件已上传,但响应会引发错误。我决定通过我的服务器传输请求以解决 CORS 缺点。
关于javascript - 如何从Web服务器上传文件到Puush?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17633586/