简体   繁体   English

ASP.NET MVC3 Uploadify-HTTP错误500

[英]ASP.NET MVC3 Uploadify - HTTP Error 500

I cant' seem to work uploadify on ASP.NET MVC3, I searched a lot and the code below seem to work fine, but not for me. 我似乎无法在ASP.NET MVC3上使用uploadify,我进行了很多搜索,下面的代码似乎正常运行,但对我而言不行。 When I try and upload it via html uploading method it works, not so much with uploadify. 当我尝试通过html上载方法将其上载时,它可以正常工作,而uploadify则不行。 All libraries are included correctly. 正确包含了所有库。

<!-- Not working, HTTP ERROR 500 -->
<input id="file" type="file" name="file" />
<script type="text/javascript">
    $(document).ready(function () {
        $('#file_upload').uploadify({
            // I tried to remove "/" at the start, does not help
            'uploader': '/Scripts/u/uploadify.swf',
            'script': '/home/upload',
            'cancelImg': '/Scripts/u/cancel.png',
            'folder': '/upload',
            'auto': true,
            'onError': function (event, ID, fileObj, errorObj) {
                alert(errorObj.type + ' Error: ' + errorObj.info);
            }
        });
    });
</script>

<!-- Working fine -->
<form action="home/upload" method="post" enctype="multipart/form-data">
  <label for="file">Filename:</label>
  <input type="file" name="file" id="file" />
  <input type="submit" />
</form>

Home Controller Action 家庭控制器动作

[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
    var fileName = Path.GetFileName(file.FileName); // Object reference not set to an instance of an object. I get this if I try to upload via uploadify
    file.SaveAs(Server.MapPath("~/upload/") + fileName);
    return Content(fileName);
}

Didn't you debug your code? 您没有调试代码吗? Didn't you notice that the file action argument is always null when the Upload action is hit? 您是否没有注意到点击“ Upload操作时,“ file action”参数始终为null? Your action argument is called file , so you need to specify that using the fileDataName option: 您的操作参数称为file ,因此您需要使用fileDataName选项指定该fileDataName

'fileDataName' : 'file',

By default uploadify uses Filedata , so if you don't want to specify this name you could also adapt your action argument name to match this: 默认情况下,uploadify使用Filedata ,因此,如果您不想指定此名称,则还可以调整操作参数名称以匹配此名称:

[HttpPost]
public ActionResult Upload(HttpPostedFileBase fileData)
{
    var fileName = Path.GetFileName(fileData.FileName);
    fileData.SaveAs(Path.Combine(Server.MapPath("~/upload/"), fileName));
    return Content(fileName);
}

Also make sure that the ~/upload folder exists on your server. 另外,请确保服务器~/upload存在~/upload文件夹。 It doesn't when you create a new ASP.NET MVC application. 创建新的ASP.NET MVC应用程序时不会。

Another problem that I would like to point out with your code is that you have hardcoded absolutely all urls in your javascript. 我想在您的代码中指出的另一个问题是,您已经在JavaScript中对所有网址进行了硬编码。 That's very bad and chances are that your application won't work when you deploy it in a virtual directory, say for example IIS. 这非常糟糕,并且当您将应用程序部署在虚拟目录(例如IIS)中时,您的应用程序将无法运行。

In ASP.NET MVC you should always use url helpers when dealing with urls, just like that: 在ASP.NET MVC中,处理URL时应始终使用URL帮助器,如下所示:

<script src="@Url.Content("~/Scripts/u/jquery.uploadify.v2.1.4.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/u/swfobject.js")" type="text/javascript"></script>

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

<script type="text/javascript">
    $(document).ready(function () {
        $('#file_upload').uploadify({
            'uploader': '@Url.Content("~/Scripts/u/uploadify.swf")',
            'script': '@Url.Action("upload", "home")',
            'cancelImg': '@Url.Content("~/Scripts/u/cancel.png")',
            'folder': '@Url.Content("~/upload")',
            'auto': true,
            'onError': function (event, ID, fileObj, errorObj) {
                alert(errorObj.type + ' Error: ' + errorObj.info);
            }
        });
    });
</script>

Is use 正在使用

HttpPostedFileBase file = Request.Files[0]; 

in my controller with Uploadify 在我的控制器中与Uploadify

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM