使用后送存储在数据库文件中 [英] Using backload to store files in database
问题描述
我'尝试使用后送( https://github.com/blackcity/Backload )上传图片到目前我们正在建设一个MVC应用程序。它应该能够存储在数据库中的图像,但我没有运气找到一个演示这个功能的例子。
I'am trying to use backload (https://github.com/blackcity/Backload) to upload images to a mvc application we are currently building. It is supposed to be able to store images in database but I had no luck finding an example that demonstrates this features.
人有运气呢?
感谢
推荐答案
我也有类似的要求,并没有希望使用的EntityFramework,所以这是我如何处理它:
I had a similar requirement and didn't want to use the entityframework, so this is how I handled it :
自定义控制器:
public async Task<ActionResult> UploadImage()
{
FileUploadHandler handler = new FileUploadHandler(Request, this);
handler.StoreFileRequestFinished += handler_StoreFileRequestFinished;
ActionResult result = await handler.HandleRequestAsync();
return result;
}
事件处理方法:
void handler_StoreFileRequestFinished(object sender, StoreFileRequestEventArgs e)
{
//I know I am only expecting 1 file...
var file = e.Param.FileStatus.Single();
//Call my service layer method to insert the image data
//You could base64 encode the stram here and insert it straight in the db
service.InsertProductImage(
int.Parse(e.Param.CustomFormValues["ProductID"]),
file.FileName,
file.FileUrl,
Server.MapPath(new Uri(file.FileUrl).PathAndQuery),
int.Parse(e.Param.CustomFormValues["FileTypeID"]),
int.Parse(e.Param.CustomFormValues["ColourID"]),
Current.User().UserID
);
}
上传的Javascript鉴于
Javascript upload in view
$('#Form_FocusGraphic').fileupload({
url: "/Product/UploadImage",
acceptFileTypes: /(jpg)|(jpeg)|(png)|(gif)$/i,
fileInput: $('input#FocusGraphicInput'),
maxFileSize: 5000000, //5mb
formData: [{ name: 'ProductID', value: '@Model.ProductID' }, { name: 'FileTypeID', value: '3' }, { name: 'ColourID', value: '0' }, { name: 'uploadContext', value: "3;0" }],
start: function (e, data) {
$('img#FocusImage').attr('src', '/Content/img/largeSpinner.gif');
},
done: function (e, data) {
var obj = jQuery.parseJSON(data.jqXHR.responseText);
$('img#FocusGraphic').attr('src', obj.files[0].url);
}
});
那么,是什么做的是发送到控制器(通过在一些特定的自订参数)的请求,然后附上一个自定义事件的FileUploadHandler来调用StoreFileRequestFinished我的自定义处理程序,pretty简单的实际。
So what is does is send a request to the controller ( passing in some specific custom params ) and then I attach a custom event to the FileUploadHandler to call my custom handler on "StoreFileRequestFinished", pretty simple actually.
这篇关于使用后送存储在数据库文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!