@TOC
1: Display of pictures
add on list page
{
field: 'cityImg',
title: '城市图片',
formatter: function (value, row, index) {
console.log("aa"+value);
return '<img src="http://localhost:8080/ShadowLine/'+value+'" width="150" height="100">';
}
},
Note that my path is a network path, and changing it to the local absolute path did not succeed.
2: Uploading pictures
When generating the code, select the upload control in the picture column
Code:
<div class="form-group">
<label class="col-sm-3 control-label">电影图片:</label>
<div class="col-sm-8">
<input type="hidden" name="filmImg" th:field="*{filmImg}">
<div class="file-loading">
<input class="form-control file-upload" id="filmImg" name="file" type="file">
</div>
</div>
</div>
$(".file-upload").each(function (i) {
var val = $("input[name='" + this.id + "']").val()
$(this).fileinput({
'uploadUrl': ctx + 'common/upload1',
initialPreviewAsData: true,
initialPreview: [val],
maxFileCount: 1,
autoReplace: true
}).on('fileuploaded', function (event, data, previewId, index) {
$("input[name='" + event.currentTarget.id + "']").val(data.response.url)
}).on('fileremoved', function (event, id, index) {
$("input[name='" + event.currentTarget.id + "']").val('')
})
$(this).fileinput('_initFileActions');
});
If this framework comes with a file upload method.
inside CommonController
/**
* Common upload request
/
@PostMapping("/common/upload")
@ResponseBody
public AjaxResult uploadFile(MultipartFile file) throws Exception
{ try { /
*
* This place is only responsible for uploading and not responsible for saving and saving is in your specific business All i you test in this location is useless
*/
// upload file path
String filePath = "D:\Environment\workspace\ShadowLine\WebContent\imgs\city\cityImg\";
System.out.println("Upload The path of the image is: "+filePath);
// Upload and return the new file name
String fileName=FileUploadUtils.upload(filePath, file);
System.out.println("上传图片的名字是:"+fileName);
String url = "imgs/city/cityImg/"+fileName;
System.out.println("url是"+url);//这个地方是存在数据库里图片的那个字段
AjaxResult ajax = AjaxResult.success();
ajax.put("fileName", fileName);
ajax.put("url", url);
return ajax;
}
catch (Exception e)
{
return AjaxResult.error(e.getMessage());
}
}
Effect picture: