MVC3中的Uploadify不起作用

时间:2012-04-12 02:55:57

标签: asp.net-mvc-3 razor uploadify

无法做到这一点。当我点击“上传文件”时,绝对没有任何反应,因为我没有看到任何闪光渲染到屏幕上。我相信这与jquery有某种关系,但我不确定。请帮忙!如果有人可以给我发送一个简单的VS2010解决方案,其中up​​loadify正在gmail.com的infinitimods实际运作,我会更感激!非常感谢!

My Layout file:

    <!DOCTYPE html>
    <html>
    <head>
        <title>@ViewBag.Title</title>
        <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
        <link type="text/css" rel="Stylesheet" media="screen" href="/Scripts/uploadify/uploadify.css" />
        <script type="text/javascript" src="/Scripts/uploadify/swfobject.js"></script>
        <script type="text/javascript" src="/Scripts/uploadify/jquery.uploadify.v2.1.4.min.js"></script>
        <script type="text/javascript" src="/Scripts/uploadify/jquery-1.4.2.min.js"></script>
    </head>

    <body>
        @RenderBody()
    </body>
    </html>



My index file:

    @{
        ViewBag.Title = "Index";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

    <h2>Index</h2>

    @using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {        
        <script type="text/javascript">
            $(document).ready(function () {

                $("#file_upload").uploadify({
                    'uploader': '~/Scripts/uploadify/uploadify.swf',
                    'cancelImg': '~/Scripts/uploadify/images/cancel.png',
                    'buttonText': 'Upload foto',
                    'script': '/Home/UploadFiles',
                    'folder': '/Content/upload',
                    'fileDesc': 'Image Files',
                    'fileExt': '*.jpg;*.jpeg;*.gif;*.png; *.txt;',
                    'scriptData': { 'thisGuid': $("input#Id").val() },
                    'multi': false,
                    'auto': true,
                    'onError': function (event, queueID, fileObj, errorObj) {
                        alert("Error!!! Type: [" + errorObj.type + "] Info [" + errorObj.info + "]");
                    }              
                });

                $("#btnSave").button().click(function (event) {
                    $('#file_upload').uploadifyUpload();
                });


            }); 

        </script>

        <input id="file_upload" type="file" />

        <input type="button" id="btnSave" value="Upload file" />

        <input id="Id" name="Id" type="hidden" value="5168e-yada-yada" />
    }

My controller:

public class HomeController : Controller
{
    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public ActionResult Index()
    {
        return View("Index");
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="fileData"></param>
    /// <param name="form"></param>
    /// <returns></returns>
    [HttpPost]
    public string UploadFile(HttpPostedFileBase fileData, FormCollection form)
    {
        return "ok";
    }
}

2 个答案:

答案 0 :(得分:1)

Uploadify需要jQuery。这意味着您需要在uploadify脚本之前包含jQuery脚本 。如果您查看了javascript调试控制台,您会看到此错误。

所以,布局:

<!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Scripts/uploadify/uploadify.css")" type="text/css" rel="stylesheet" media="screen" />
    <script type="text/javascript" src="@Url.Content("~/Scripts/uploadify/swfobject.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/Scripts/uploadify/jquery-1.4.2.min.js")"></script>
    <script type="text/javascript" src="@Url.Content("~/Scripts/uploadify/jquery.uploadify.v2.1.4.min.js")"></script>
</head>

<body>
    @RenderBody()
</body>
</html>

控制器:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult UploadFile(HttpPostedFileBase fileData, string thisGuid)
    {
        if (fileData != null && fileData.ContentLength > 0)
        {
            var appData = Server.MapPath("~/app_data");
            var file = Path.Combine(appData, Path.GetFileName(fileData.FileName));
            fileData.SaveAs(file);
        }
        return Json(new { status = true });
    }
}

和视图:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<script type="text/javascript">
    $(document).ready(function () {
        $('#file_upload').uploadify({
            'uploader': '@Url.Content("~/Scripts/uploadify/uploadify.swf")',
            'cancelImg': '@Url.Content("~/Scripts/uploadify/images/cancel.png")',
            'buttonText': 'Select photo',
            'script': $('form').attr('action'),
            'fileDesc': 'Image Files',
            'fileExt': '*.jpg;*.jpeg;*.gif;*.png; *.txt;',
            'multi': false,
            'auto': false,
            'onError': function (event, queueID, fileObj, errorObj) {
                alert("Error!!! Type: [" + errorObj.type + "] Info [" + errorObj.info + "]");
            }
        });

        $('form').submit(function () {
            $('#file_upload').uploadifySettings('scriptData', { thisGuid: $('#id').val() });
            $('#file_upload').uploadifyUpload();
            return false;
        });
    }); 
</script>


<h2>Index</h2>

@using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{        
    <input id="id" name="id" type="hidden" value="5168e-yada-yada" />
    <input id="file_upload" type="file" name="fileData" />
    <input type="submit" value="Upload file" />
}

如果您想在用户选择照片时启动上传过程,您可以删除表单和提交按钮,并将auto属性设置为true

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<script type="text/javascript">
    $(document).ready(function () {
        $('#file_upload').uploadify({
            'uploader': '@Url.Content("~/Scripts/uploadify/uploadify.swf")',
            'cancelImg': '@Url.Content("~/Scripts/uploadify/images/cancel.png")',
            'buttonText': 'Select photo',
            'script': $('form').attr('action'),
            'fileDesc': 'Image Files',
            'fileExt': '*.jpg;*.jpeg;*.gif;*.png; *.txt;',
            'multi': false,
            'auto': true,
            'scriptData': { thisGuid: $('#id').val() },
            'onError': function (event, queueID, fileObj, errorObj) {
                alert("Error!!! Type: [" + errorObj.type + "] Info [" + errorObj.info + "]");
            }
        });
    }); 
</script>


<h2>Index</h2>

<input id="id" name="id" type="hidden" value="5168e-yada-yada" />
<input id="file_upload" type="file" name="fileData" />

另外,请不要忘记查看uploadify documentation以更好地了解不同选项的用途,还可以看到一些示例。

答案 1 :(得分:0)

你需要解决asp.net和flash的错误。

这篇文章曾帮我一次:Working around flash cookie bug

也许这是一个更好的解决方案:jquery file upload