gpt4 book ai didi

angularjs - 分段表单上传图片和 Json

转载 作者:行者123 更新时间:2023-12-04 14:44:00 26 4
gpt4 key购买 nike

有点卡在这个上,需要使用多部分形式将图像和 json 一起上传.. 不确定如何发送内容类型标题或上传图像.. 想我需要转换为 blob.. 目前我只是发送我从文件输入字段获得的数据。

任何建议都会很棒谢谢

        $http({
method: 'POST',
url: URL,
headers: { 'Content-Type': false },

transformRequest: function (data) {

console.log(data);

var formData = new FormData();
formData.append("formatteddata", angular.toJson(data.model));


formData.append('media', Image)

return formData;
},

data: { model: shoutoutData, image: shoutoutImage}
}).
success(function (data, status, headers, config) {

alert("success!");

}).
error(function (data, status, headers, config) {

alert("failed!");

});

最佳答案

Here is the code what i did in my project to upload image and data:- 
HTML PAGE :-
<form role="form" name="myForm" ng-submit="submitCuisine(myForm.$valid)" novalidate>
<div class="form-group" ng-class="{ 'has-error' : myForm.name.$invalid && myForm.name.$touched }">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name"
placeholder="Name of cuisine" ng-model="dataform.name" required>
</div>
<div class="form-group" ng-class="{ 'has-error' : myForm.description.$invalid && myForm.description.$touched }">
<label for="description">Description</label>
<input type="text" class="form-control" id="description" name="description"
placeholder="Description for cuisine" ng-model="dataform.description" required>
</div>
<div class="form-group" ng-class="{ 'has-error' : myForm.category.$invalid && myForm.category.$touched }">
<label for="description">Category</label>
<select class="form-control" ng-model="dataform.category" id="category" name="category" required>
<option>Veg</option>
<option>Non-veg</option>
</select>
</div>
<div class="form-group" ng-class="{ 'has-error' : myForm.subcategory.$invalid && myForm.subcategory.$touched }">
<label for="description">Sub-Category</label>
<select class="form-control" ng-model="dataform.subcategory" id="subcategory" name="subcategory" required>
<option>Main Course</option>
<option>Staters</option>
</select>
</div>
<div class="form-group" ng-class="{ 'has-error' : myForm.price.$invalid && myForm.price.$touched }">
<label for="description">Price</label>
<span class="fa fa-dollar"></span>
<input type="number" class="form-control" id="price" name="price"
placeholder="Price" ng-model="dataform.price" required>
</div>
<div class="form-group">
<label for="description">Image</label>
<input type="file" file-input="files" name="file"/>
</div>
<button class="btn btn-primary" type="submit" ng-disabled="myForm.$invalid"> Submit</button>
</form>



Controller:-
$scope.submitCuisine=function(isvalid){
if(isvalid){
var fd=new FormData();
angular.forEach($scope.files,function(file){
fd.append('file',file);
});

fd.append('formdata',JSON.stringify($scope.dataform));

$http.post('admin/managecuisineAdd',fd,{
transformRequest:angular.identity,
headers:{'Content-type':undefined}
}).success(function(data){
$scope.status=data;
$scope.itemlist.push(data)
$scope.message="New Dish Added Successfully"
});
}
}

Directive :-
myApp.directive("fileInput",['$parse',function($parse){
return{
restrict:'A',
link:function(scope,ele,attrs){
ele.bind('change',function(){
$parse(attrs.fileInput).
assign(scope,ele[0].files)
scope.$apply()
});
}
}
}]);

普拉克:- http://plnkr.co/edit/yPNA0ij3Dn37tsI9w7Z2?p=preview
检查 Firebug 中的帖子标题,您会发现它以加密形式显示图像,并在其末尾显示数据。

关于angularjs - 分段表单上传图片和 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28023703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com