我正在VS 2010上开发应用程序。在调试期间,当我上传图像文件时,我得到IO错误。这是图像
以下是我的剧本
<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";
}
答案 0 :(得分: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的文件的限制,可以使用httpRuntime
element在web.config中配置。因此,如果您要上传大文件,请确保已将maxRequestLength
和executionTimeout
设置调整为您想要允许的所需最大值:
<system.web>
<httpRuntime maxRequestLength="102400" executionTimeout="3600" />
</system.web>
答案 1 :(得分: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);
}