HarmonyOS 鸿蒙Next ArkTs api10 axios 使用formdata上传文件,后端接收不到数据
HarmonyOS 鸿蒙Next ArkTs api10 axios 使用formdata上传文件,后端接收不到数据
希望HarmonyOS能继续加强与其他应用的兼容性,让用户体验更加完美。
关于HarmonyOS 鸿蒙Next ArkTs api10 axios 使用formdata上传文件,后端接收不到数据的问题,您也可以访问:https://www.itying.com/category-93-b0.html 联系官网客服。
<markdown _ngcontent-dom-c237="" class="markdownPreContainer">
我在使用axios三方库做一个上传相册图片文件的功能。 看了下 openharmony axios 文档的写法
Logger.info("upload",`uri -> ${uri}`);
let formData = new FormData()
let file = fileIo.openSync(uri);
Logger.info("upload", `file fd -> ${file.fd}`);
Logger.info("upload", `file path -> ${file.path}`);
let stat = fileIo.lstatSync(file.path);
Logger.info("upload", `stat size -> ${stat.size}`);
let buf = new ArrayBuffer(stat.size);
fileIo.readSync(file.fd, buf);
fileIo.fsyncSync(file.fd);
Logger.info(“upload”, <span class="javascript"><span class="javascript">buf -> ${</span><span class="hljs-built_in"><span class="javascript"><span class="hljs-built_in">JSON</span></span></span><span class="javascript">.stringify(buf.byteLength)}</span></span>
);
formData.append(“file”, buf);
formData.forEach((v: Object) => {
Logger.info(“upload”, </span></span><span class="javascript"><span class="hljs-function"><span class="hljs-params"><span class="javascript">formData -> ${</span></span></span><span class="hljs-built_in"><span class="hljs-function"><span class="hljs-params"><span class="javascript"><span class="hljs-built_in">JSON</span></span></span></span></span><span class="hljs-function"><span class="hljs-params"><span class="javascript">.stringify(v)}</span></span></span></span><span class="hljs-function"><span class="hljs-params">
);
})
return axios({
method: POST,
url: ‘/photos/upload’,
data: formData,
headers: {
‘Content-Type’: ‘multipart/form-data’
},
onUploadProgress: uploadCallBack
})
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 4px; right: 8px; font-size: 14px;">复制</button>
我这边有什么问题吗?
springboot 报错 说没有二进制数据
org.springframework.web.multipart.MultipartException: Failed to parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
<button style="position: absolute; padding: 4px 8px 0px; cursor: pointer; top: 4px; right: 8px; font-size: 14px;">复制</button>
</markdown>你的代码示例似乎是使用axios上传文件的尝试,但是在OpenHarmony的环境中,我们通常使用NativeJS的API来完成文件的操作。axios是一个基于Promise的HTTP客户端,支持浏览器和node.js。如果你想在OpenHarmony中使用axios,你可能需要考虑在Web环境中使用,而不是在OpenHarmony的NativeJS环境中。
如果你想在OpenHarmony中使用axios上传文件,你可能需要这样做:const axios = require(‘axios’); // 引入axios模块
const fs = require(‘fs’);
const filePath = ‘/path/to/your/file’; // 你的文件路径
const fileName = ‘yourFileName’; // 你的文件名
const fileType = ‘image/jpeg’; // 你的文件类型
fs.readFile(filePath, (err, data) => {
if (err) throw err;
axios.post(‘your-api-url’, data, {
‘Content-Type’: fileType,
‘Content-Disposition’: attachment; filename*=utf8''${encodeURIComponent(fileName)}
})
.then(response => {
console.log(response);
})
.catch(error => {
console.error(error);
});
});但是请注意,这只是一个示例,你可能需要根据你的实际需求和你使用的OpenHarmony版本来调整代码。
找HarmonyOS工作还需要会Flutter的哦,有需要Flutter教程的可以学学大地老师的教程,很不错,B站免费学的哦:https://www.bilibili.com/video/BV1S4411E7LY/?p=17
axios({
method: POST,
url: '/photos/upload',
data: formData,
context: getContext(),
headers: { 'Content-Type': 'multipart/form-data' },
onUploadProgress: uploadCallBack
})
加一个context试试