Javascript 将图像上传ajax与表单提交ajax相结合
我有像这样的图像上传ajaxJavascript 将图像上传ajax与表单提交ajax相结合,javascript,angularjs,ajax,forms,Javascript,Angularjs,Ajax,Forms,我有像这样的图像上传ajax $scope.uploadFile = function(){ var file = $scope.myFile; console.log(file); var uploadUrl = "/api/upload_image";//It will also goes to '/api/get_data' //fileUpload.uploadFileToUrl(file, uploadUrl); var fd = new For
$scope.uploadFile = function(){
var file = $scope.myFile;
console.log(file);
var uploadUrl = "/api/upload_image";//It will also goes to '/api/get_data'
//fileUpload.uploadFileToUrl(file, uploadUrl);
var fd = new FormData();
fd.append('file', file);
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.success(function(e){
console.log("Success");
})
.error(function(e){
console.log("Error");
});
};
像这样调用提交表单ajax
$http({
url: "/api/get_data",
method: "POST",
dataType:"json",
data:JSON.stringify(this.formData)
}).success(function(data) {
console.log("Success");
}).error(function(error) {
console.log("Error");
});
两者都是独立工作的,但是如何将这两个ajax组合成一个,即提交ajax,第二个
或者有没有办法在第二个ajax中发布图像数据,我正在使用angular+laravel5.2
我在角度视图中的文件输入是
<input type="file" file-model="myFile">
谢谢。您可以像这样组合这两个ajax,发布图像和表单数据,试试这个
var file = $scope.myFile;
var fd = new FormData();
fd.append('file', file);
fd.append('formData', JSON.stringify(this.formData));
$http({
url: "/api/get_data",
method: "POST",
dataType:"json",
transformRequest: angular.identity,
headers: {'Content-Type': undefined},
data:fd
}).success(function(data) {
要检索
formData
,您需要在服务器端脚本中解码json。有两个不同的url。因此,将两者结合起来可能不是ide。相反,您可以使用promise和chain multiplepromise@user2181397很抱歉使用了不同的url,我需要它作为一个url/api/get_data
上传图像的格式是什么?你想建造什么?你的站点有图像上传器吗?@AlanSutherland,我正在尝试提交任何格式的图像的正常格式数据。好的,你可能需要先将其分解为二进制,你要提交到哪里?