获取HTTP错误,而使用uploadify在Asp.net MVC应用程序 [英] Getting HTTP Error while using uploadify on Asp.net MVC application

查看:198
本文介绍了获取HTTP错误,而使用uploadify在Asp.net MVC应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  <一href=\"http://stackoverflow.com/questions/1877644/uploadify-show-error-message-from-http-response\">Uploadify:显示来自HTTP响应错误信息


我开发在2010年VS应用程序在调试过程中,当我上传一个图片文件,我得到IO错误。这里是图像

以下是我的剧本

 &LT;脚本类型=文/ JavaScript的&GT;
$(文件)。就绪(函数(){
    $('#file_upload')。uploadify({
        '上传':'/uploadify/uploadify.swf',
        '脚本':'主页/上传',
        cancelImg':'/uploadify/cancel.png',
        文件夹:内容/图片',
        fileDesc':'图像文件',
        fileExt':'。* .JPG; * JPEG,GIF *; * PNG',
        自动:真实
    });
});
&LT; / SCRIPT&GT;

以下是我的控制器code

 公共字符串上传(HttpPostedFileBase FILEDATA)
    {
        变种文件名= this.Server.MapPath(〜/内容/图片/+ System.IO.Path.GetFileName(fileData.FileName));
        fileData.SaveAs(文件名);
        返回确定;
    }


解决方案

这是很难说的问题可能是您的code什么。你将不得不进行调试。

下面是一个完整的工作的例子:

 公共类HomeController的:控制器
{
    公众的ActionResult指数()
    {
        返回查看();
    }    [HttpPost]
    公众的ActionResult上传(HttpPostedFileBase FILEDATA)
    {
        如果(FILEDATA =空&放大器;!&放大器; fileData.ContentLength大于0)
        {
            变种文件名=使用Server.Mappath(〜/内容/图片/+ Path.GetFileName(fileData.FileName));
            fileData.SaveAs(文件名);
            返回JSON(真);
        }
        返回JSON(假);
    }
}

查看(〜/查看/主页/ Index.cshtml

  @ {
    布局= NULL;
}
&LT;!DOCTYPE HTML&GT;
&LT; HTML和GT;
&LT; HEAD&GT;
    &LT;间的charset =UTF-8/&GT;
    &LT;链接HREF =@ Url.Content(〜/ uploadify / uploadify.css)的rel =stylesheet属性类型=文/ CSS/&GT;
&LT; /头&GT;
&LT;身体GT;
    &LT; D​​IV ID =file_upload&GT;&LT; / DIV&GT;    &LT;脚本的src =@ Url.Content(〜/脚本/ jQuery的-1.5.1.min.js)TYPE =文/ JavaScript的&GT;&LT; / SCRIPT&GT;
    &LT;脚本的src =@ Url.Content(〜/ uploadify / swfobject.js)TYPE =文/ JavaScript的&GT;&LT; / SCRIPT&GT;
    &LT;脚本的src =@ Url.Content(〜/ uploadify / jquery.uploadify.v2.1.4.js)TYPE =文/ JavaScript的&GT;&LT; / SCRIPT&GT;
    &LT;脚本类型=文/ JavaScript的&GT;
        $('#file_upload')。uploadify({
            '上传':'@ Url.Content(〜/ uploadify / uploadify.swf)',
            '脚本':'@ Url.Action(上传,家),
            cancelImg':'@ Url.Content(〜/ uploadify / cancel.png)',
            文件夹:@ Url.Content(〜/内容/图像),
            fileDesc':'图像文件',
            fileExt':'。* .JPG; * JPEG,GIF *; * PNG',
            自动:真实
        });
    &LT; / SCRIPT&GT;
&LT; /身体GT;
&LT; / HTML&GT;

请确保〜/内容/图片文件夹,你是哪个服务器上存在上传或控制器动作会抛出异常。您还会注意到,如何在我的例子中的所有URL网址,通过引用佣工其中硬编码来代替。通过这种方式,应用程序是保证工作无论是在IIS或在本地虚拟目录中主持。

我已经使用了uploadify 2.1.4版,我下载了,把内容在〜/ uploadify 文件夹在服务器上。

这是你应该知道的另一件事是它可以在web.config中使用的 的httpRuntime 元素。所以,如果你nitend上传大文件请确保您已经调整了的maxRequestLength executionTimeout 设置为所需的最大值你要允许:

 &LT;&的System.Web GT;
    &LT;的httpRuntime的maxRequestLength =102400executionTimeout =3600/&GT;
&LT; /system.web>

Possible Duplicate:
Uploadify: show error message from HTTP response

I am developing the application on VS 2010. During debugging, When i upload an image file i get IO Error. Here is the image

Following is my script

<script type="text/javascript">
$(document).ready(function () {
    $('#file_upload').uploadify({
        'uploader': '/uploadify/uploadify.swf',
        'script': 'Home/Upload',
        'cancelImg': '/uploadify/cancel.png',
        'folder': 'Content/Images',
        'fileDesc': 'Image Files',
        'fileExt': '*.jpg;*.jpeg;*.gif;*.png',
        'auto': true
    });
});
</script>

Following is my controller code

    public string Upload(HttpPostedFileBase fileData)
    {
        var fileName = this.Server.MapPath("~/Content/Images/" + System.IO.Path.GetFileName(fileData.FileName));
        fileData.SaveAs(fileName);
        return "ok";
    }

解决方案

It's hard to say what the problem might be with your code. You will have to debug it.

Here's a full working example:

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

    [HttpPost]
    public ActionResult Upload(HttpPostedFileBase fileData)
    {
        if (fileData != null && fileData.ContentLength > 0)
        {
            var fileName = Server.MapPath("~/Content/Images/" + Path.GetFileName(fileData.FileName));
            fileData.SaveAs(fileName);
            return Json(true);
        }
        return Json(false);
    }
}

View (~/Views/Home/Index.cshtml):

@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <link href="@Url.Content("~/uploadify/uploadify.css")" rel="stylesheet" type="text/css" />
</head>
<body>
    <div id="file_upload"></div>

    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/uploadify/swfobject.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/uploadify/jquery.uploadify.v2.1.4.js")" type="text/javascript"></script>
    <script type="text/javascript">
        $('#file_upload').uploadify({
            'uploader': '@Url.Content("~/uploadify/uploadify.swf")',
            'script': '@Url.Action("Upload", "Home")',
            'cancelImg': '@Url.Content("~/uploadify/cancel.png")',
            'folder': '@Url.Content("~/content/images")',
            'fileDesc': 'Image Files',
            'fileExt': '*.jpg;*.jpeg;*.gif;*.png',
            'auto': true
        });    
    </script>
</body>
</html>

Make sure that the ~/Content/Images folder to which you are uploading exists on your server or the controller action will throw an exception. You will also notice how in my example all urls are referenced through url helpers instead of hardcoding them. This way the application is guaranteed to work no matter whether it is hosted inside a virtual directory in IIS or locally.

I have used the uploadify version 2.1.4 that I downloaded and put the contents in the ~/uploadify folder on the server.

Another thing that you should be aware of is the limit of files that can be posted to ASP.NET which could be configured in web.config using the httpRuntime element. So if you nitend to upload large files make sure you have adjusted the maxRequestLength and executionTimeout settings to the desired maximum values you want to allow:

<system.web>
    <httpRuntime maxRequestLength="102400" executionTimeout="3600" />
</system.web>

这篇关于获取HTTP错误,而使用uploadify在Asp.net MVC应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆