簡體   English   中英

結合圖像上傳ajax和表單提交ajax

[英]Combine image upload ajax with form submit ajax

我有這樣的圖像上傳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 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");
    });
};

然后像這樣調用submit form 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組合在一起,以發布圖像和formData,並嘗試使用此方法。

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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM