2
ng-file-upload를 사용하여 기본 단일 파일을 내 서버에 업로드합니다.서버로 보낼 때 파일 객체가 비어 있습니다. ng-file-upload
파일을 선택하고 내가 선택한 파일 이름을보고 파일 서비스 개체 ($scope.myFiles[0]
)를 내 각도 서비스에 전달할 수 있습니다. 그러나 내 HTTP 데이터 속성을 파일로 채울 때 dev 도구에서 HTTP 요청을 검사하면 파일이 비어 있습니다.
컨트롤러 :
UploadService.upload({ data: { file: $scope.myFiles[0], 'category': $scope.selectedCategory}})
.success(function (data) {
////
////
UploadService : 위의와
app.factory('UploadService', function ($http, $q) {
return {
upload: function (something) {
console.log(something);
return $http({
method: 'POST',
url: '/upload',
data: something
});
}
}
});
, 개발 도구 HTTP 요청에서 검사, 난을 참조하십시오
Request Payload:
{data: {file: {}, category: "Friend Card"}}
data:
{file: {}, category: "Friend Card"}
category:"Friend Card"
file:{}
콘솔의 출력에서 찾고있다. 로그 (무언가)를 통해 파일을 볼 수 있습니다 :
특히
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:48
Content-Type:image/jpeg
Cookie:_ga=GA1.1.2023678669.1463291637; _gat=1
Host:localhost:8585
Origin:http://localhost:8585
Referer:http://localhost:8585/pages/
: 내가 개발 도구에서 볼 수
return $http({
method: 'POST',
url: '/upload',
headers: {
'Content-Type': something.data.file.type
},
data: {
"file": something.data.file,
"category": something.data.category
}
});
: 콘텐츠 형식 : 이미지/JPEG 그 정도 6,
{
$hashKey:"object:107"
lastModified:1465716430000
lastModifiedDate:Sun Jun 12 2016 08:27:10 GMT+0100 (BST)
name:"Postcard.JPG"
size:522622
type:"image/jpeg"
webkitRelativePath:""
}
을 통해 헤더 형식을 가지고 기능을 변경 한 그러나 파일은 여전히 빈 개체입니다.
사용'Upload.upload ({파일 URL : ..., 파일을}) 서버 대신 $ http – danial
@ 대니얼 - 고마워,하지만 이해가 안 돼 .. 대신 http의 ?? 모범이 있습니까? –
문서에는 많은 예제가 있습니다. Upload.upload ({ method : 'POST', url : '/ upload', data : something }); – danial