标签:
public string UploadPhotoUrl(HttpPostedFileBase uploadFile)
{
MessageResult result = new MessageResult();
string domain = this.TheVisitorInfo.DomainUniqueName;
string userLoginName = this.TheVisitorInfo.LoginName;
Guid g = this.TheVisitorInfo.Guid;
result.Message = MyStringHelper.default_domain_logo_url;
if (string.IsNullOrEmpty(domain))
{
return @"<script>alert(‘域唯一名不能为空!‘)</script>";
}
if (string.IsNullOrEmpty(userLoginName))
{
return @"<script>alert(‘用户名不能为空!‘)</script>";
}
if (uploadFile != null)
{
//存入文件
if (uploadFile.ContentLength > 0)
{
using (BinaryReader r = new BinaryReader(uploadFile.InputStream))
{
try
{
if (!MyFileTypeChecker.CheckExtension(r, new string[] { "png", "jpg", "gif", "bmp" }))
{
r.Close();
uploadFile.InputStream.Close();
return @"<script>alert(‘您选择上传的文件被系统认定为非法文件!请重新选择文件!‘)</script>";
}
string lastPre = "jpg";
string newFileName = ChangeName(uploadFile.FileName, g.ToString(), lastPre);
//保存文件
string filepath= Server.MapPath("~/content/default/images/") + "PhotoTest" + newFileName;
uploadFile.SaveAs(filepath);
//保存数据库
result.Message = "/content/default/images/" + "Photo" + newFileName;
siteProfileBo.UpdatePhotoUrl(domain, result.Message, userLoginName);
//截取缩略图
var img = Image.FromFile(filepath);
Image thumbnail = img.GetThumbnailImage(180, 120, null, IntPtr.Zero);
thumbnail.Save(Server.MapPath(result.Message));
thumbnail.Dispose();
img.Dispose();
if (System.IO.File.Exists(filepath))
{
//如果存在则删除
System.IO.File.Delete(filepath);
}
}
catch (Exception e)
{
}
finally
{
r.Close();
uploadFile.InputStream.Close();
}
}
}
}
return "<script>window.parent.getUrlCallBack(\‘" + result.Message + "\‘)</script>";
}
标签:
原文地址:http://www.cnblogs.com/zhtbk/p/4530741.html