【问题标题】:Converting uploadify code from ASP.NET 4 to MVC 3将 uploadify 代码从 ASP.NET 4 转换为 MVC 3
【发布时间】:2011-07-28 16:49:59
【问题描述】:

我有一个可以上传单个和多个文件的 ASP.NET 4 工作上传代码。 一旦uploadify发送它们,我使用HttpHandler在服务器端处理上传。

我应该如何在 MVC 3 中解决这个问题?

我是否需要编写不同的操作来处理单个文件和多个文件的上传?

我只需要代码的结构,以便我可以在服务器上处理发布的文件。

这是我在 ASP.NET 4 中的代码:

$(function () {
        $('#file_upload').uploadify({
            'uploader': '/content/uploadify/uploadify.swf',
            'script': '/content/uploadify/uploadimg.ashx', <<-- httphandler here
            'scriptData': { 'auth': auth, 'sid': sid, 'aid': '', 'pid': 0, 'multi': 1 },
            'cancelImg': '/content/uploadify/cancel.gif',
            'folder': '/content/uploadify/uploads',
            'auto': false,
            'multi': true,
            'queueSizeLimit': 10,
            'sizeLimit': 2359296,
            'fileExt': '*.jpg;*.jpeg',
            'fileDesc': 'Photo Files ( .jpg )',
            'displayData': 'speed',
            'expressInstall': '/content/uploadify/expressInstall.swf',
            'removeCompleted': false,
            'wmode': 'transparent',
            'hideButton': true,
            'height': 33,
            'width': 156

            , 'onSelectOnce': function (event, data) { //code omitted }

            , 'onError': function (e, fid, fo, eo) { //code omitted }

            , 'onComplete': function (e, q, f, r, d) { //code omitted }

            , 'onAllComplete': function (e, d) {
                //code omitted
            }
        });
    });


uploadimg.ashx:
public class uploadimg : IHttpHandler, IRequiresSessionState
{
    public void ProcessRequest (HttpContext context) 
    {
         //code omitted
    }
}

【问题讨论】:

    标签: asp.net-mvc-3 uploadify


    【解决方案1】:

    您需要做的只是添加一个动作——修改自here

    public ActionResult UploadFiles()
    {
        var r = new List<ViewDataUploadFilesResult>();
    
        foreach (string file in Request.Files)
        {
            HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
            if (hpf.ContentLength == 0)
                continue;
            string savedFileName = Path.Combine(
                AppDomain.CurrentDomain.BaseDirectory, 
                Path.GetFileName(hpf.FileName));
            hpf.SaveAs(savedFileName);
        }
        return new EmptyResult();
    }
    

    然后使用您的 uploadify 脚本相应地更改您的上传路径。

    【讨论】:

    • 这是为 MVC 2 编写的,也可以在 MVC 3 中使用。不过我不确定。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    相关资源
    最近更新 更多