【发布时间】:2020-04-05 04:01:29
【问题描述】:
我正在尝试将文件从手机上传到 Azure Blob 存储,作为带有 SAS 的 BlockBlob。我可以让文件上传,但下载后无法打开。该文件以某种方式损坏。我认为这是一个内容类型问题,但我尝试了几种不同的方法来更改为内容类型。到目前为止没有任何效果。
我的代码:
FileInfo _fileInfo = await filePicker(); // get the file path and file name
// my getUploadInfo fires a call to my backend to get a SAS.
// I know for a fact that this works because my website uses this SAS to upload files perfectly fine
UploadInfo uploadInfo = await getUploadInfo(_fileInfo.fileName, _fileInfo.filePath);
final bytes = File(_fileInfo.filePath).readAsBytesSync();
try {
final response = await myDio.put(
uploadInfo.url,
data: bytes,
onSendProgress:
(int sent, int total) {
if (total != -1) {
print((sent / total * 100).toStringAsFixed(0) + "%");
}
},
options:
dioPrefix.Options(headers: {
'x-ms-blob-type': 'BlockBlob',
'Content-Type': mime(_fileInfo.filePath),
})
);
} catch (e) {
print(e);
}
这段代码上传文件就好了。但我无法打开文件,因为它已损坏。起初,我认为这是一个 Content-Type 问题,所以我尝试将内容类型标头更改为:application/octet-stream 和 multipart/form-data。这行不通。
我也试过了
dioPrefix.FormData formData =
new dioPrefix.FormData.fromMap({
'file': await MultipartFile.fromFile(
_fileInfo.filePath,
filename: _fileInfo.fileName,
)
});
...
final response = await myDio.put(
uploadInfo.url,
data: formData, // This approach is recommended on the dio documentation
onSendProgress:
...
但这也会损坏文件。它已上传,但我无法打开它。
我已经能够使用此代码成功上传文件,但使用这种方法我无法获得任何类型的响应,所以我不知道它是否成功上传(另外,我可以't get the progress of the upload):
try {
final data = imageFile.readAsBytesSync();
final response = await http.put( // here, response is empty no matter what i try to print
url,
body: data,
headers: {
'x-ms-blob-type': 'BlockBlob',
'Content-Type': mime(filePath),
});
...
任何帮助将不胜感激。谢谢
【问题讨论】:
-
尝试使用 Azure 存储资源管理器下载文件。如果它也损坏了,那么就是您的上传代码损坏了。 PS:我从未听说过这样的事情(SAS 损坏了 blob 内容。我 99.999% 确定这是您的下载/上传代码)
-
在使用 Azure 存储资源管理器时也已损坏。抱歉,标题误导了,我改一下。我也 99.99999% 确定这是上传代码。我只是不确定它的哪一部分搞砸了
-
很遗憾,没有用于 Flutter 的 SDK。您最好尝试使用 REST gist.github.com/gregjhogan/ef37c38371193c8e9d08d867c05ad210 PS:使用代理来捕获请求并与您当前的请求进行比较,您将能够确定有什么不同
-
嗨@wei,每当我使用这种方法时,我都会得到一个 404,知道我的 url 是正确的,并且我无法使用带有 SAS 令牌的连接字符串.. 有什么帮助吗?跨度>
标签: azure flutter dart azure-blob-storage dio