我在Node.js中有一个项目,该项目生成一个文档,并使用axios库通过HTTP将其发送到Django REST API。 API正在使用MultiPartFormDataParser
。
我遇到了问题,因为我的API未收到任何文件或数据。我想我没有通过axios正确发送文件。
这里有一些我项目的代码片段:
documentation.js
import API from "./api";
export class DocumentationGateway {
async uploadDocument(policyID, folderName, data) {
const options = {
headers: data.getHeaders()
};
return API().post(`policies/${policyID}/documents/${folderName}:upload/`, data, options);
}
}
stream-converter.js
export class StreamConverter {
static toStream(buffer, name) {
const fs = require("fs");
fs.writeFileSync(name, buffer);
return fs.createReadStream(name);
}
}
upload-documentation.js
import { eventBus } from "src/event-bus";
const FormData = require("form-data");
export class UploadDocument {
constructor(documentationService) {
this.documentationService = documentationService;
}
async execute(policyID, content, fileName, folder) {
const data = new FormData();
data.append("file", content, fileName);
try {
const response = await this.documentationService.uploadDocument(policyID, folder, data);
// TODO check the response and do something
eventBus.emit("document-uploaded", response);
} catch (error) {
throw (error);
}
}
}