Swift:使用 alamofire 上传时出现在图像源中的内容处置



我正在使用以下代码在 azure 服务器上上传图像文件,成功上传文件。(我可以在服务器上看到它(

self.manager.upload(multipartFormData: { (multipart) in

multipart.append(fileData, withName: "file",fileName: "uploading.png" , mimeType: "image/png")

}, to: url.description, method: .put, headers: ["x-ms-blob-type":"BlockBlob","Content-type": "multipart/form-data","Content-Disposition" :"form-data"]) { encodingResult in
switch encodingResult {
case .success(let upload, _, _):
upload.response { answer in
print("statusCode: (String(describing: answer.response?.statusCode))")
comp(true,"Documnet Uploaded Successfully")
}

upload.uploadProgress { progress in
//call progress callback here if you need it
}
case .failure(let encodingError):
print("multipart upload encodingError: (encodingError)")
comp(false,"Unable to Upload the documnet")
}
}

但是当我下载文件(手动或通过代码(时。图像未打开。它说的

它可能已损坏或使用预览无法识别的文件格式。

当我在记事本中打开文件时。以下行显示在每个上传图像的顶部。没有这些行的文件是"正常"。我不知道为什么每次都会自动添加这些。

-

-alamofire.boundary.5243a11a13e8a8f4

内容处置:表单数据;名称="文件";文件名="上传.png">

内容类型:图像/png

请提出解决方案。

不是直接的答案,但这里有一些建议你可以看看。

1.在上传到 azure 之前检查图像是否正常。

2.通过代码将其上传到 azure 时,请检查是否可以在 Azure 门户中查看正确的图像内容(在 Azure 门户 -> Blob 存储中>单击图像 ->然后选择"编辑"(。

3.我认为根本原因可能是由于不正确的请求标头。

通过代码将图像上传到 azure 时,请使用 fiddler 等工具来检查其请求/响应标头。

然后手动将图像上传到 azure,并使用 fiddler 检查其请求/响应标头。

然后,您可以比较标头(代码与手动(,以找出区别,然后您可以相应地对代码进行一些更改。

一切正常。请求,标头和所有内容。 由于多部分请求而出现问题。这是一个简单的 请求将照片或 PDF 上传到 Azure,就像将其上传到 FTP.这不是多部分请求。

最新更新