问题
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";
}
回答1:
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>
回答2:
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 }
});
In 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);
}
来源:https://stackoverflow.com/questions/8358223/getting-http-error-while-using-uploadify-on-asp-net-mvc-application