Fine Uploader-服务器端代码保存上传的文件,但客户端显示“上传失败”消息

时间:2013-05-24 19:13:52

标签: fine-uploader

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

代码背后:

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>

1 个答案:

答案 0 :(得分: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();
    }
}

an example using ASP.NET MVC C# server examples repository可以提供一些帮助。

此外,在开发分支上有a server-side README,它可以准确表明Fine Uploader的有效JSON响应是什么。

相关问题