[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, new { enctype = "multipart/form-data", id = "mws-Validate", target = "UploadTarget", @class = "mws -形" })) {
<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>
}