php receive picture file,Angular 2 - 图片上传,文件未上传到后端(未列出错误)(Angular 2 - Image upload , File not gettin...

Angular 2 - 图片上传,文件未上传到后端(未列出错误)(Angular 2 - Image upload , File not getting uploaded to backend (no error listed))

我试图以角度2上传图像,我没有列出错误,但文件添加详细代码的位置没有变化如下

**.ts**

............

onFileChange(event) {

if(event.target.files.length > 0) {

let file = event.target.files[0];

this.fileName = file.name;

this.imageUpload['upload'] = this.fileName;

this.form.get('upload').setValue(file);

console.log(file);

console.log('filename',this.fileName);

}

}

private prepareSave(): any {

let inputVal = new FormData();

inputVal.append('upload', this.form.get('upload').value,this.imageUpload['upload']);

console.log(inputVal);

return inputVal;

}

onSubmit() {

this.imageUpload['upload'] = this.fileName;

const formModel = this.prepareSave();

this.loading = true;

var body = formModel;

let url = this.ImageUploadURL;

let headers = new Headers({ 'Content-Type': 'multipart/form-data' ,'Authorization': 'Bearer ' + this.accountsService.accessToken });

let options = new RequestOptions({ headers: headers });

console.log(options);

console.log(body);

console.log(url);

// In a real-world app you'd have a http request / service call here like

return this.http.post(url, body, options)

.catch(error => Observable.throw(error))

.subscribe(

data => console.log('success'),

error => console.log(error)

)

}

...............

在控制台中获得成功 yqcKE.png

该方法使用网址发布, {upload:“”}

在网络即时通讯中 4c971bd21348622a4b22cd35cd3ef646.png

可能是所需部分没有变化的错误?

还有其他方法吗?

I am trying to upload an image in angular 2 , i have no error listed but there is no change in the position the file added detailed code is given below

**.ts**

............

onFileChange(event) {

if(event.target.files.length > 0) {

let file = event.target.files[0];

this.fileName = file.name;

this.imageUpload['upload'] = this.fileName;

this.form.get('upload').setValue(file);

console.log(file);

console.log('filename',this.fileName);

}

}

private prepareSave(): any {

let inputVal = new FormData();

inputVal.append('upload', this.form.get('upload').value,this.imageUpload['upload']);

console.log(inputVal);

return inputVal;

}

onSubmit() {

this.imageUpload['upload'] = this.fileName;

const formModel = this.prepareSave();

this.loading = true;

var body = formModel;

let url = this.ImageUploadURL;

let headers = new Headers({ 'Content-Type': 'multipart/form-data' ,'Authorization': 'Bearer ' + this.accountsService.accessToken });

let options = new RequestOptions({ headers: headers });

console.log(options);

console.log(body);

console.log(url);

// In a real-world app you'd have a http request / service call here like

return this.http.post(url, body, options)

.catch(error => Observable.throw(error))

.subscribe(

data => console.log('success'),

error => console.log(error)

)

}

...............

in console its getting successcb3f57e5ad3fe4d90178277c9783745b.png

the method is post with url and { upload:"" }

In the network im getting4c971bd21348622a4b22cd35cd3ef646.png

what might be the error that there is no change on desired portion?

is there any other way ?

原文:https://stackoverflow.com/questions/49461150

更新时间:2020-02-14 11:48

最满意答案

在Angular中,当使用多部分表单数据并且您正在使用FormData对象时,您不能指定Content-Type标头。

通过这样做,您正在阻止http堆栈将其设置为必须包含边界值的正确值 。

只需删除内容类型标题,让格式化程序为您处理,您应该没问题。

In Angular, when using multipart form-data and you are using FormData object, you MUST NOT specifiy the Content-Type header.

By doing so you are preventing the http stack to set it to the proper value which MUST include the boundary value.

Just remove the content type header and let the formatter handle that for you, and you should be fine.

2018-03-24

相关问答

移动文件的目标需要是服务器的绝对路径,即 move_uploaded_file( $_FILES["file"]["tmp_name"], $_SERVER['DOCUMENT_ROOT'] ."/uploads/" . $_FILES["file"]["name"]);

The destination of the moved file needs to be an absolute path to the server, i.e. move_uploaded_file( $_FILES["f

...

我们可以编写一个控制器来接收文件 class ImportRoleProfileController {

SpringSecurityService springSecurityService;

def importUpload() {

User currentUser = springSecurityService.getCurrentUser();

def file = request.getFile('file')

Work

...

你可以试试这个: $request->file('photo')->move($destination_path, $file_name);

如果需要,在路径和文件名之间添加DIRECTORY_SEPARATOR,并将该文件复制到新位置 copy($destination_path.$file_name, $new_path.$new_file_name);

You can try this: $request->file('photo')->move($destination_path, $

...

要通过AngularJS上传图像,您需要做一些额外的工作。 您可以使用像nervgh / angular-file-upload (或其他一些库)这样的库,也可以自己动手。 有关进一步参考,请参阅egghead的本教程 For uploading images via AngularJS, you need to do little extra work. You can either use some library like nervgh/angular-file-upload (or som

...

在Angular中,当使用多部分表单数据并且您正在使用FormData对象时,您不能指定Content-Type标头。 通过这样做,您正在阻止http堆栈将其设置为必须包含边界值的正确值 。 只需删除内容类型标题,让格式化程序为您处理,您应该没问题。 In Angular, when using multipart form-data and you are using FormData object, you MUST NOT specifiy the Content-Type header.

...

这是因为您的图像二进制数据未发送。 然后我使用这个https://github.com/danialfarid/ng-file-upload和头部的一点点配置。 它有效。 It's happen because your binary data of image not sent. Then I use this https://github.com/danialfarid/ng-file-upload and little bit configuration for headers. And i

...

您缺少的是角度自动转换您的请求数据,这是FormData不需要的。 您可以通过将transformRequest: angular.identity,添加到您的请求对象来停止此操作。 What you're missing is that angular transforms your request data automatically, which you do not want with FormData. You can stop this by adding transformReque

...

save.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View arg0) {

// Locate the image in res >

//Bitmap bitmap = BitmapFactory.decodeFile("picturePath");

// Convert it

...

header("Access-Control-Allow-Origin: *");

if ($_SERVER['REQUEST_METHOD'] !== 'POST') {

echo json_encode(array('status' => false));

exit;

}

$path = 'uploads/';

if (isset($_FILES['file'])) {

$originalName = $_FILES['file']['name'];

$e

...

$basePath = str_replace(DIRECTORY_SEPARATOR.'protected', "", str_replace('frontend', '', Yii::app()->basePath));

$uploadDir = 'backend/www/images/';

$uploadedFile->saveAs($basePath .$uploadDir. $fileName);

希望这可以帮助 ;) $basePath = str_replace(DIRECTORY

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值