【问题标题】:Pass parameter to WebAPI through Uploadify通过 Uploadify 将参数传递给 WebAPI
【发布时间】:2014-05-06 17:46:51
【问题描述】:

如何将对象从 uploadify 传递到 webapi 控制器?我不确定如何正确使用 ScriptData 参数。

我试过这个:

    $("#fileInput").uploadify({

            'uploader': '/api/files/Addfiles/',
            'swf'       : '/uploadify/uploadify.swf',
            'cancelImage' : '/uploadify/uploadify-cancel.png',
            'auto': true,
            'folder': "/uploads",
            'scriptData'     : {'product': null }               
        }); 

这是我的控制器:

    public Task<HttpResponseMessage> AddFiles(Product product )
    {
    }

如果我从我的控制器中删除 Product 参数,控制器会被调用(点击“选择文件”)并且一切正常。但是当我尝试传递一个参数时,它不会被调用。我的路线模板是:

  api/{controller}/{action/{id}

【问题讨论】:

  • 有人可以帮我解决这个问题吗?
  • 好的。经过一番调查,我发现 uploadify will 可以与 MVC 控制器操作一起愉快地工作,但似乎没有将 scriptData 作为请求 URL 的一部分传递(根据 Fiddler2)。我自己的解决方案是将任何额外的值组合到 settings.formData 中,它们确实会通过。
  • 您能否展示您的 WEB API 控制器操作的其余部分,以便我弄清楚为什么我的测试平台对 WEB API 根本不起作用?

标签: jquery asp.net-web-api uploadify


【解决方案1】:

好的。经过一番调查,我发现uploadify 与标准MVC控制器操作一起工作,但似乎没有将scriptData作为请求URL的一部分传递(根据Fiddler2)。

作为替代方案,您可以修改 formData 设置以回发其他信息:

示例视图:

<div class="myuploader">
    <input id="file_upload" type="file" name="file_upload" />
    <input id="uploadfiles" type="button" value="Upload files" />
</div>

<script type="text/javascript">
    $(function () {
        $('.myuploader').uploadify({
            swf: '/content/uploadify.swf',
            uploader: 'home/upload',
            auto: true,
            formData: {product: 456}
            });

控制器上传动作:

    public ActionResult Upload()
    {
        HttpPostedFileBase oFile = Request.Files["Filedata"];
        if (oFile != null)
        {
            string sDirectory = Server.MapPath("~/Files/");
            string filename = Path.GetFileName(oFile.FileName);

            // You can pick up any parameter set in the formData member of the plugin using its name
            string testParam = Request.Form["product"];   // This gets 456

            // Do something with the file here
            return Content("1");
        }
        else
        {
            return Content("0");
        }
    }
}

跟进:

我仍然无法使用标准 WebApi 控制器让 Uploadify 正常工作。

顺便说一句:由于我们需要 SWF 和 HTML 5 支持,我们将 uploadify 和商业 uploadifive 包装在我们自己的插件中,它可以根据浏览器功能的需要动态转换参数。我们还让它自动提取任何隐藏的字段值并将它们组合到 formData 中。您可能想自己尝试类似的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-18
    • 2012-02-17
    • 2015-11-14
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多