2011-12-02 79 views
0

可能重复:
Uploadify: show error message from HTTP response获取HTTP错误,而使用uploadify在Asp.net MVC应用程序

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

enter image description here

以下是我的脚本

<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> 

以下是我的控制器代码

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

回答

1

很难说的问题可能与你的代码是什么。你将不得不调试它。

这里是一个完整的工作示例:

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); 
    } 
} 

视图(~/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> 

确保~/Content/Images文件夹,你是哪个服务器上存在上传或控制器中的操作将抛出一个例外。您还会注意到,在我的示例中,所有url都是通过url助手引用的,而不是对它们进行硬编码。通过这种方式,应用程序无论是托管在IIS中的虚拟目录还是本地托管都可以保证工作。

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

,你应该知道的另一件事是,可以发布到ASP.NET可能在web.config中使用httpRuntime element配置文件的限制。所以,如果你nitend上传大文件请确保您已经调整了maxRequestLengthexecutionTimeout设置到所需的最大值,要允许:

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

我想你的代码,但没有露面....可能是你忘了写输入元素.....会在一段时间 –

+0

@Pankaj Upadhyay添加它们,让你有在我的代码没有输入元素。只有它使用的Uploadify插件,并转化成一个按钮,让你选择文件,并将其上传到服务器的单个''

。如果你在运行这个例子时遇到麻烦,你可以把你的代码发给我,这样我就可以看看它有什么问题了。 –

+0

我创建了一个新的项目,并试图您的代码....它的工作原理实际上.....在我的项目我是在插入一些其他性质的HTTP形式file_upload DIV .....会考虑,看看什么错误.....感谢队友 –

1
it's help you. 
var auth = "@(Request.Cookies[FormsAuthentication.FormsCookieName]==null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value)"; 
    var ASPSESSID = "@(Session.SessionID)"; 

    $("#uploadifyLogo").uploadify({ 
     ... 
     'scriptData': { 'ASPSESSID': ASPSESSID, 'AUTHID': auth } 
    }); 

在Global.asax中:

protected void Application_BeginRequest(object sender, EventArgs e) 
    { 
     /* we guess at this point session is not already retrieved by application so we recreate cookie with the session id... */ 
     try 
     { 
      string session_param_name = "ASPSESSID"; 
      string session_cookie_name = "ASP.NET_SessionId"; 

      if (HttpContext.Current.Request.Form[session_param_name] != null) 
      { 
       UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]); 
      } 
      else if (HttpContext.Current.Request.QueryString[session_param_name] != null) 
      { 
       UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString[session_param_name]); 
      } 
     } 
     catch 
     { 
     } 

     try 
     { 
      string auth_param_name = "AUTHID"; 
      string auth_cookie_name = FormsAuthentication.FormsCookieName; 

      if (HttpContext.Current.Request.Form[auth_param_name] != null) 
      { 
       UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]); 
      } 
      else if (HttpContext.Current.Request.QueryString[auth_param_name] != null) 
      { 
       UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]); 
      } 

     } 
     catch 
     { 
     } 
    } 

    private void UpdateCookie(string cookie_name, string cookie_value) 
    { 
     HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name); 
     if (null == cookie) 
     { 
      cookie = new HttpCookie(cookie_name); 
     } 
     cookie.Value = cookie_value; 
     HttpContext.Current.Request.Cookies.Set(cookie); 
    } 
相关问题