如何将文件上传到网址服务器,其名称在网址中

时间:2017-08-18 19:45:08

标签: javascript php jquery ajax

我想通过应用将手机上的图像上传到服务器。

使用PHP我总是要提交表单,但我不能,因为我只是打开一个链接: http://xxx.yy/?file=C:/xxx/yyy/zzz.png

我还有其他可能性将图像从Android应用程序上传到网络服务器吗?

如何使用URL中提供的文件路径上传文件?

1 个答案:

答案 0 :(得分:0)

您可以使用BlobXMLHttpRequest()利用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));