我有一个jqgrid,我想上传一个文件,我得到了以下代码
colModel: [
{ name: "id", index:"id", key: true,width: 30,editable: false },
{ name: "name", index:"name", width: 100,editable: true },
{ name: "fileToUpload",
editoptions: {
enctype: "multipart/form-data"
},
edittype:'file',
index: 'fileToUpload',
width: 150,
align: "left",
editable: true },
],
我使用jqgrid - upload a file in add/edit dialog
中的功能我的问题是我不知道如何使用ajaxfileupload函数的url文件将此文件保存在数据库中,有人能给我看一个例子吗?
谢谢!
答案 0 :(得分:0)
这是我的工作范例:
colModel:
[
{
name: 'photo1',
label: 'Фото1',
index: 'photo1',
search: false,
editable: true,
edittype: 'file',
editoptions: {
enctype: "multipart/form-data"
},
align: 'center',
},
],
然后 - 我使用了回调:
afterSubmitCell: function (serverResponse, rowId, cellName, value, iRow, iCol) {
var fileCell = (+rowId + 1) + '_' + cellName;
var grid = $("#reports");
var fileContainer = document.getElementById(fileCell).files;
if (fileContainer !== null) {
if (fileContainer.length > 0) {
var file = fileContainer[0];
var form = new FormData();
form.append(cellName, file);
form.append('id', rowId);
$.ajax({
url: "/api/reports/save-file",
type: 'POST',
data: form,
processData: false,
contentType: false,
success: function (data) {
grid.jqGrid('setGridParam', {datatype: 'json'})
.trigger('reloadGrid', [{page: 1}]);
}
});
}
}
return [true, serverResponse]
},
然后将文件保存在我的后端部分(Laravel):
if ($request->hasFile('photo1')) {
$file = $request->file('photo1');
if ($file->isValid()) {
$fileName = rand(1000, 9999) . "." . $file->getClientOriginalExtension();
//Put file on the server
if (!empty($report)) {
$report->attachFile($file, $fileName);
$report->photo1 = $fileName;
$response['status'] = $report->save();
}
}
}