1

我有一个 MVC2 应用程序,我正在尝试使用 Fine-Uploader 插件。当我在后面运行我的代码时,它会保存我上传的文件。但是,在浏览器中显示的是 Upload Failed。我不确定我在这里缺少什么。我的代码如下:

后面的代码:

public void UploadFiles()
        {
            try
            {
                if (Request.Files.Count > 0)
                {
                    foreach (string file in Request.Files)
                    {
                        HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;

                        if (hpf.ContentLength == 0)
                        {
                            continue;
                        }

                        string filename = Path.GetFileName(hpf.FileName);
                        string path = Path.Combine(Server.MapPath(ConfigurationManager.AppSettings["AttachmentPath"]), filename);
                        hpf.SaveAs(path);

                    }
                }

            }
            catch (Exception e)
            {
                //Do something
            }

        }

母版页:

<script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"></script>
    <script src="<%=Url.Content("~/Scripts/jquery.fineuploader-3.5.0.js") %>" type="text/javascript"></script>
    <script src="<%=Url.Content("~/Scripts/jquery.fineuploader-3.5.0.min.js") %>" type="text/javascript"></script>

标记页面:

<div id="manual-fine-uploader"></div>
    <div id="triggerUpload" class="btn btn-primary" style="margin-top: 10px;">
      <i class="icon-upload icon-white"></i> Upload now
    </div>

<script type="text/javascript">
    $(document).ready(function () {
        var manualuploader = new qq.FineUploader({
            element: $('#manual-fine-uploader')[0],
            request: {
                endpoint: 'Home/UploadFiles'
            },
            autoUpload: false,
            text: {
                uploadButton: '<i class="icon-plus icon-white"></i> Select Files'
            }
        });

        $('#triggerUpload').click(function () {
            manualuploader.uploadStoredFiles();
        });enter code here
    });
</script>
4

1 回答 1

1

Fine Uploader 需要一个有效的 JSON 响应来指示上传是否成功。

成功的上传响应必须具有:

{ "success": true }

让 Fine Uploader 知道它有效。您可以在回复中添加任何您想要的内容,但如果不指明“成功”,Fine Uploader 会认为上传失败。


我要做的是为您的UploadFiles函数添加一个返回值。有点像:

public UploadResult UploadFiles()
{
    try 
    {
        // ... save file and other things
    } 

    catch (Exception ex)
    {
        // failsauce :(
        return new UploadResult(false);
    }

    // success :)
    return new UploadResult(true);
}

哪里UploadResult很像:

public class UploadResult
{
    // This is important!
    public const string ResponseContentType = "text/plain";

    public FineUploaderResult(bool success)
    {
        _success = success;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        // Here we create the JSON Response object,
        // set the correct content-type, and finally
        // it gets built with the correct success flag.
        var response = context.HttpContext.Response;
        response.ContentType = ResponseContentType;

        response.Write(BuildResponse());
    }

    public string BuildResponse()
    {
        var response = new JObject();
        response["success"] = _success;

        // ... maybe set some other data in the response JSON

        return response.ToString();
    }
}

服务器示例存储库中有一个使用 ASP.NET MVC C#的示例,可以提供一些帮助。

此外,在开发分支上有一个服务器端 README,它准确地指出了 Fine Uploader 的有效 JSON 响应的构成。

于 2013-05-24T19:55:31.007 回答