Я студент, делающий проект, использующий Angular в качестве внешнего интерфейса и Flask в качестве внутреннего интерфейса. Я загружаю изображение во внешнем интерфейсе и отправляю его в свой бэкэнд (Flask). Как вы можете видеть в python cmd, python cmd изображение отправляется в бэкэнд (длина содержимого img печатается), но оно не входит в , если зацикливается и не сохраняетсяв папку загрузки. Я проверяю все коды, читаю все соответствующие сообщения в stackoverflow, но ничего не работает. Пожалуйста, скажите мне, что я делаю не так.
Вот мой код внешнего интерфейса HTML Код
<div class="form-group">
<input type="file" name="image" (change)="fileProgress($event)" />
</div>
<div *ngIf="fileUploadProgress">
Upload progress: {{ fileUploadProgress }}
</div>
Код моего component.ts
ngOnInit() {
}
fileProgress(fileInput: any) {
this.fileData = <File>fileInput.target.files[0];
this.preview();
}
preview() {
// Show preview
this.previewUrl = null;
var mimeType = this.fileData.type;
if (mimeType.match(/image\/*/) == null) {
return;
}
var reader = new FileReader();
reader.readAsDataURL(this.fileData);
reader.onload = (_event) => {
this.previewUrl = reader.result;
}
}
onSubmit() {
const formData = new FormData();
formData.append('file', this.previewUrl);
this.http.post('http://127.0.0.1:5000/uploadimage', formData)
.subscribe(res => {
console.log(res);
console.log('data passed to backend');
//this.count = res.count;
//this.uploadedFilePath = res.data.filePath;
alert('SUCCESS !!');
});
}
Вот мой Backend (python, flaskКод:
@app.route('/uploadimage', methods=['GET', 'POST'])
@cross_origin()
def upload_file():
print(request.files)
content_length = request.content_length
print(f"Content_length : {content_length}")
if request.method == 'POST':
# check if the post request has the file part
if 'image' not in request.files:
print('No file part')
return jsonify ({ " text" : " No File"})
file = request.files['file']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
print('No selected file')
return jsonify ({ " text" : " File hasn't selected"})
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
return jsonify ({ " text" : " File Uploaded Successfully"})