【问题标题】:Using backload to store files in database使用 backload 将文件存储在数据库中
【发布时间】:2013-08-09 10:07:50
【问题描述】:

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

有人遇到过这种情况吗?

谢谢

【问题讨论】:

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


【解决方案1】:

我有类似的要求,不想使用实体框架,所以我是这样处理的:

自定义控制器:

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

【讨论】:

    【解决方案2】:

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

    在这里阅读:https://github.com/blackcity/Backload/wiki/Configuration#database-configuration-element

    【讨论】:

      猜你喜欢
      • 2017-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-28
      • 1970-01-01
      • 2010-11-07
      • 2011-08-20
      • 1970-01-01
      相关资源
      最近更新 更多