Я пытаюсь загрузить файл на сервер и получаю код ответа об ошибке -1.
Это для приложения NativeScript, созданного с помощью Angular, и я использую плагин NativeScript HTTP Background.
component.ts
onCreateRecipe() {
console.log(this.imageUris);
const recipeForm = {
name: this.form.get('name').value,
description: this.form.get('description').value,
photo: this.imageUris[0],
ingredients: this.form.get('ingredients').value,
// steps: this.form.get('steps').value,
// tag: 'poultry recipe'
type: this.recipeType
};
console.log(recipeForm);
this.apiService.postRecipe(recipeForm).subscribe(res => console.log(res));
}
service.ts
postRecipe(recipe) {
const session = bghttp.session('image-upload');
const subject = new Subject<any>();
const request = {
url: `${this.apiURL}/recipe/1`,
method: 'POST',
headers: {
"Content-Type": "application/octet-stream"
},
description: 'test'
};
let task: bghttp.Task;
const params = [
{ name: 'name', value: recipe.name },
{ name: 'description', value: recipe.description },
{ name: 'photo', filename: recipe.photo, mimeType: 'image/png' },
{ name: 'ingredients', value: JSON.stringify(recipe.ingredients) }
];
console.log(params);
task = session.multipartUpload(params, request);
task.on('responded', (event: any) => {
if (event.data && event.data.error) {
subject.error(event.data);
} else {
subject.next(event.data);
}
});
// task.on('error', (event) => subject.error(event));
task.on('error', (e) =>console.log("received " + e.responseCode + " code."));
return subject;
}
Примечание: ингредиенты - это FormArray, поэтому я должен использовать JSON.Stringify, чтобы передать его вплагин.recipe.photo - это путь к файлу изображения.
Я ожидаю, что все данные, включая файл изображения, будут загружены на сервер, но сейчас загружается только изображение.