我想通过应用将手机上的图像上传到服务器。
使用PHP我总是要提交表单,但我不能,因为我只是打开一个链接: http://xxx.yy/?file=C:/xxx/yyy/zzz.png
我还有其他可能性将图像从Android应用程序上传到网络服务器吗?
如何使用URL中提供的文件路径上传文件?
答案 0 :(得分:0)
您可以使用Blob
或XMLHttpRequest()
利用fetch()
将FileReader
转换为Blob
,从本地文件系统请求文件data URI
},将.href
元素的<a>
属性设置为带有查询字符串的服务器路径,其中data URI
是查询值,在.click()
元素上调用<a>
。在服务器进程中,GET
处的查询字符串参数请求处理文件的data URI
表示。
const a = document.createElement("a");
document.body.appendChild(a);
const reader = new FileReader;
reader.onload = () => {
a.href = `/path/to/server?file=${reader.result}`;
a.click();
}
fetch("/path/to/local/file")
.then(response => response.blob())
.then(blob => reader.readAsDataURL(blob))
.catch(err => console.error(err));