Using backload to store files in database

江枫思渺然 提交于 2019-12-06 11:15:02

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!