我正在使用ajax表单将多个文件上传到服务器。这就是我的表格看起来像
@using (@Ajax.BeginForm("SaveProjectSummary", "Project", new { id = Model.ProjectId }, new AjaxOptions { OnSuccess = "Add_OnCompleteSummarySave()" }, new { enctype = "multipart/form-data" }))
我可以引导我进行所需的操作,但在服务器上的Request.Files中找不到任何内容。只是想确定我是否正在使用这个似乎不对我的权利所以任何帮助将不胜感激。
答案 0 :(得分:2)
可悲的是,Ajax.BeginForm不能用于上传文件。
http://www.uploadify.com/forum/#/discussion/1685/uploadify-and-ajax/p1
答案 1 :(得分:2)
另一个很棒的上传器组件是PlUpload
它确实:
仅供参考,.NET中用于处理来自PlUpload的发布请求的服务器端代码:
var chunk = Request.Form["chunk"] != null ? int.Parse(Request.Form["chunk"]) : 0;
var fileName = Request.Form["name"] ?? "";
//open a file, if our chunk is 1 or more, we should be appending to an existing file, otherwise create a new file
var fs = new FileStream(Server.MapPath("/files/" + fileName), chunk == 0 ? FileMode.OpenOrCreate : FileMode.Append);
//write our input stream to a buffer
var buffer = new Byte[Request.Files[0].InputStream.Length];
Request.Files[0].InputStream.Read(buffer, 0, buffer.Length);
//write the buffer to a file.
fs.Write(buffer, 0, buffer.Length);
fs.Close();
如果您热衷于推送自己的组件,请查看现代浏览器上提供的文件系统API。它允许您获取文件内容二进制文件并使用AJAX调用上传它
答案 2 :(得分:0)
[HttpPost] public JsonResult UploadImage(HttpPostedFileWrapper imageFile) {
if (imageFile == null || imageFile.ContentLength == 0 || imageFile.ContentLength > 205168)
{
return new JsonResult
{
ContentType = "text/html",
Data = "No file was uploaded."
};
}
if (imageFile == null || imageFile.ContentLength == 0 || (imageFile.ContentType != "image/png" && imageFile.ContentType != "image/jpg" && imageFile.ContentType != "image/jpeg" && imageFile.ContentType != "image/pjpeg"))
{
return new JsonResult
{
ContentType = "text/html",
Data = "Type"
};
}
if (Session["CityId"] != null)
{
if (MdlNadal == null)
{
MdlNadal = new mdlNadal();
}
string strFilePaths = "";
int CityId = Convert.ToInt32(Session["CityId"].ToString());
string strCityName = "";
if (Session["CityName"] != null)
{
strCityName = Session["CityName"].ToString();
}
string strFileNames = imageFile.FileName.Replace(@"\", "/");
string imgPath = ConfigurationManager.AppSettings["ImagePath"].ToString().Replace("~", "");
strFileNames = strFileNames.Split('/')[strFileNames.Split('/').Length - 1];
Session["ImageName"] = strFileNames;
ViewBag.ImageName = strFileNames;
strFilePaths = Request.Url.Scheme + "://" + Request.Url.Authority + imgPath + strCityName + "" + CityId + "/" + strFileNames;
MdlNadal.UpdateCityImageByCityID(CityId, strFilePaths);
if (imageFile != null)
{
if (!Directory.Exists(Server.MapPath(Url.Content(ConfigurationManager.AppSettings["ImagePath"].ToString() + strCityName + "" + CityId))))
{
Directory.CreateDirectory(Server.MapPath(Url.Content(ConfigurationManager.AppSettings["ImagePath"].ToString() + strCityName + "" + CityId)));
}
else
{
int counts = Directory.GetFiles(Server.MapPath(Url.Content(ConfigurationManager.AppSettings["ImagePath"].ToString() + strCityName + "" + CityId))).Count();
if (counts > 1)
{
string[] StrArr = Directory.GetFiles(Server.MapPath(Url.Content(ConfigurationManager.AppSettings["ImagePath"].ToString() + strCityName + "" + CityId)));
for (int i = 0; i <= counts - 1; i++)
{
string strFileNameCheck = StrArr[i];
//strFileNameCheck = strFileNameCheck.Replace(@"\", "/");
//strFileNameCheck = strFileNameCheck.Split('/')[strFileNameCheck.Split('/').Length - 1];
try
{
System.IO.File.Delete(strFileNameCheck);
}
catch (Exception ex)
{
throw ex;
}
}
}
}
var FilePath = Path.Combine(Server.MapPath(Url.Content(ConfigurationManager.AppSettings["ImagePath"].ToString() + strCityName + "" + CityId)), strFileNames);
imageFile.SaveAs(FilePath);
}
}
return new JsonResult
{
ContentType = "text/html",
Data = "Save"
};
}
@using(Html.BeginForm(“UploadImage”,“Nadal”,FormMethod.Post, 新 { enctype =“multipart / form-data”, id =“mws-Validate”, target =“UploadTarget”, @class =“mws-form” })) {
<label class="title10">
<strong>Choose a background for this city</strong>
</label>
<div class="mws-form-item large">
<input type="file" name="imageFile" id="imageFile" onchange="return SetAttachFlag();" />
(Supported File Types : .jpg, .jpeg, .png) (Maximum Size: 200 Kb)
<iframe id="UploadTarget" name="UploadTarget" onload="UploadImage_Complete();" style="position: absolute;
left: -999em; top: -999em;"></iframe>
</div>
}
答案 3 :(得分:0)
为什么不利用HTML5中的File API。看看这些链接,指出你正确的方向。
http://www.html5rocks.com/en/tutorials/file/dndfiles/
http://timothypoon.com/blog/2011/05/10/ajax-uploading-with-html5s-file-api/