[英]Handshake Exception when trying to upload a file using MultipartRequest in flutter
[英]Using MultiPartRequest to upload file in flutter web
所以我目前正在嘗試將文件選擇器中的圖像發送到具有文件輸入的 api,這是 postman 顯示,事實是,我已經嘗試過其他帖子中的多種方式,但沒有一個適用於我,這是我從 web 瀏覽器控制台得到的錯誤,這是我嘗試使用的代碼:
var postUri = Uri.parse(constanta.getMyid());
http.MultipartRequest request = new http.MultipartRequest("POST", postUri);
http.MultipartFile multipartFile = await http.MultipartFile.fromBytes(
'data', image);
request.files.add(multipartFile);
http.StreamedResponse response = await request.send();
print(response.statusCode);
我認為主要問題不是 CORS,因為 api 日志表明已收到請求,但沒有文件附加到該請求。
附加說明:我已經從客戶端嘗試了所有方法,但我無法打開/編輯后端,因為它是第三方 api(不歸我所有)。
正如我所見,您的錯誤主要問題首先是您的服務器拒絕了數據,因為服務器不允許您上傳文件並拋出您必須首先解決的跨域訪問,然后您應該嘗試。
如果你的 postman 可以上傳圖片到服務器,那是因為 postmen 本身可以解決 Cross-Origin Acess Error,所以這里是 node.js 的解決方案
如果您的后端在 node.js 中,您可以將以下代碼注入到您的中間件中。
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'XMLHttpRequest,Origin,X-Requested-With,Content-Type,Accept,Authorization');
if (req.method === 'OPTIONS') {
res.header('Access-Control-Allow-Methods', 'PUT,POST,PATCH,DELETE,GET');
return res.status(200).json({});
}
next();
});
或者您可以使用 localhost 作為后端服務器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.