Using backload to store files in database

2019-05-27 11:44发布

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.

Anyone had luck with this?

thanks

2条回答
Luminary・发光体
2楼-- · 2019-05-27 12:05

We use the database feature in our commercial product. I have to say that we use a custom version developed for us. As far as I know the database feature is only available for the Enterprise edition along with the source code and support. It's not cheap but worth it.

Read here: https://github.com/blackcity/Backload/wiki/Configuration#database-configuration-element

查看更多
forever°为你锁心
3楼-- · 2019-05-27 12:10

I had a similar requirement and didn't want to use the entityframework, so this is how I handled it :

Custom Controller :

public async Task<ActionResult> UploadImage()
    {
        FileUploadHandler handler = new FileUploadHandler(Request, this);

        handler.StoreFileRequestFinished += handler_StoreFileRequestFinished;

        ActionResult result = await handler.HandleRequestAsync();

        return result;
    }

Event handler method :

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 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);
            }
        });

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.

查看更多
登录 后发表回答