Uno, referencia en el formulario
@inject Microsoft.AspNetCore.Hosting.IWebHostEnvironment HostingEnvironment
@section header{
@BundlerHelper.Render(HostingEnvironment.ContentRootPath, Url.Content("~/lib/fileinput/5.0.3/css/fileinput.min.css"))
@BundlerHelper.Render(HostingEnvironment.ContentRootPath, Url.Content("~/lib/fileinput/5.0.3/js/fileinput.min.js"))
}
Dos, modificación del campo de formulario
Antes de la modificación:
<label class="col-sm-2 control-label ">Product photo </label>
<div class="col-sm-4">
<input id="goodsPhoto" col="GoodsPhoto" type="text" class="form-control" />
</div>
después de la modificación:
<label class="col-sm-2 control-label ">Product photo </label>
<div class="col-sm-4">
<input id="fileinput" type="file"/>
<input id="goodsPhoto" col="GoodsPhoto" type="hidden" class="form-control" />
</div>
3. Añadir interruptor… caso en Dzjy.Util.FileHelpe
//Platform product picture
case (int)UploadFileType.PlatFormGoodsPhoto:
if (file.Length > 5 * 1024 * 1024)
{
obj.Message = "The maximum file limit is 5MB";
return obj;
}
objCheck = CheckFileExtension(Path.GetExtension(file.FileName), ".jpg|.jpeg|.gif|.png");
if (objCheck.Tag != 1)
{
obj.Message = objCheck.Message;
return obj;
}
dirModule = UploadFileType.PlatFormGoodsPhoto.ToString();
break;
Cuatro, método de adición de formulario js:
//Picture-save method
$("#fileinput").fileinput({
language: 'zh',
'uploadUrl': '@GlobalContext.SystemConfig.UploadUrl' + '@GlobalContext.SystemConfig.UploadAction' + '[email protected]()',
showPreview: false,
maxFileSize: 50,
allowedFileExtensions: ['jpg', 'jpeg', 'gif', 'png']
}).on("fileuploaded", function (event, data) {
var obj = data.response;
if (obj.Tag == 1) {
$("#goodsPhoto").val(obj.Data);
}
});
//Determine whether the image is added successfully
if (!$("#goodsPhoto").val()) {
ys.alertError('The picture was not uploaded or the upload failed');
return;
}
5. Modificaciones en el índice:
Antes de la modificación:
{ field: 'Measure', title: 'unit of measurement' },
Después de la modificación:
{
field: 'GoodsPhoto', title: 'Product Photo',
//Image viewing function of the page
formatter: function (value, row, index) {
if (row.GoodsPhoto!= "") {
var imageUrl = '@GlobalContext.SystemConfig.UploadUrl' + row.GoodsPhoto;
return '<img class="img-md" src="' + imageUrl + '" οnclick=showImage("' + imageUrl + '") />';
}
else {
var imageUrl = '';
return '<img class="img-md" src="' + imageUrl + '" οnclick=showImage("' + imageUrl + '") />';
}
}
},
.