使用backload在数据库中存储文件

时间:2013-08-07 20:42:24

标签: asp.net-mvc file-upload jquery-file-upload

我正在尝试使用backload(https://github.com/blackcity/Backload)将图像上传到我们当前正在构建的mvc应用程序。它应该能够在数据库中存储图像,但我没有运气找到一个演示这个功能的例子。

有人幸运吗?

感谢

2 个答案:

答案 0 :(得分:1)

我有类似的要求,并且不想使用entityframework,所以这就是我处理它的方式:

自定义控制器:

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

$('#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”上调用我的自定义处理程序,实际上非常简单。

答案 1 :(得分:0)

我们在商业产品中使用数据库功能。我不得不说我们使用为我们开发的自定义版本。据我所知,数据库功能仅适用于企业版以及源代码和支持。它不便宜但值得。

请在此处阅读:https://github.com/blackcity/Backload/wiki/Configuration#database-configuration-element