我有一个字符串,它是我系统上文件的路径(例如:/var/www/html/country.png)
我需要使用 javascript/angular6 将此文件上传到服务器。但问题是服务器接受要作为 html 输入文件对象或 blob 发送的文件。
例如这段代码上传文件成功
var form = new FormData();
form.append("filedata", $('input[type=file]')[0].files[0]);
form.append("overwrite", "true");
var settings = {
"async": true,
"crossDomain": true,
"url": "https://www.example.com/api/upload",
"method": "POST",
"headers": {
"authorization": "Basic XXXXXXX=="
},
"processData": false,
"contentType": false,
"mimeType": "multipart/form-data",
"data": form
}
$.ajax(settings).done(function (response) {
console.log(response);
});
有什么方法可以做到这一点。
最佳答案
如果这是可能的,那将是一个巨大的安全漏洞。想象一下任何网站都能够通过字符串访问文件(提示:/etc/passwd
在每个 Linux 发行版上都位于同一位置)。
简而言之,不可能。用户必须在 <input type="file" />
打开的上传对话框中主动选择文件,您无法控制此过程。
关于javascript - 如何将文件路径转换为 blob 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52199562/