我正在使用 angular-file-upload 将图像与表单数据一起上传到我的服务器。我已经能够一次成功地完成一个图像。
我的问题是,在我的表单中,我需要上传两张图片,使用单独的输入(以及发送其他表单数据)。我使用单独输入的原因是我的一张图片是缩略图,另一张是英雄图片。我需要能够区分它们并将每个文件的文件路径插入到我的数据库中它们各自的列中。
我通读了这个 github 问题,其中介绍了如何使用相同的输入上传多个文件,但我找不到任何关于使用不同的输入上传多个文件的信息。也许我误解了他们。
现在,如果我尝试选择标题图像,它只会更改缩略图图像的值。
这是我的表格:
<form>
...(other text form inputs above)...
<div class="form-group">
<label class="col-sm-2 control-label"> Teaser</label>
<div class="col-sm-8>
<input class="form-control" filestyle="" accept=".png,.gif,.jpg,.jpeg" data-button-text="Choose image" type='file' data-classbutton='btn btn-default' data-classinput="form-control inline" nv-file-select='' uploader='uploader' options="{'field': 'thumbnail_url'}"
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label"> Header Image</label>
<div class="col-sm-8>
<input class="form-control" filestyle="" accept=".png,.gif,.jpg,.jpeg" data-button-text="Choose image" type='file' data-classbutton='btn btn-default' data-classinput="form-control inline" nv-file-select='' uploader='uploader' options="{'field': 'header_url'}"
</div>
</div>
</form>
以下是我将文件发送到服务器的方式(在 onCompleteAll 回调中,我将表单数据发送到服务器):
$scope.uploader = new FileUploader({
url: '/laravel/public/admin/events/thumbnail_url/file_upload',
alias: 'files',
headers: {
// 'X-CSRF-Token': n/a
},
onBeforeUploadItem: function(item) {
/* Laravel file name alias */
item.alias = 'file';
},
onSuccessItem: function(fileItem, response, status, headers) {
$scope.newEventForm[fileItem.field] = '/laravel/public/uploads/events/' + response.filename;
},
onCompleteAll: function() {
/* Now ready to save form data */
AdminEvent.save($scope.newEventForm).$promise.then(function(response) {
$scope.events.push(response.data);
toaster.pop('success', 'Added', 'Event added successfully.');
});
}
});