monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><td>
<asp:FileUpload ID="fuImages" runat="server" /><asp:Button ID="btnUpload" runat="server"
Text="上传" OnClick="btnUpload_Click" /><asp:Image ID="imgPreview" runat="server" Height="50" Width="140" AlternateText="暂无图片" />
</td>
.CS
protected void btnUpload_Click(object sender, EventArgs e)
{
if (ViewState["VendorID"].ToString() == "0")
{
myShare.WebMessageBox(this.Page, "图片上传对应的资料不存在,无法上传,请在列表页面选择一笔资料再进行上传");
}
string mPath = uploadImage(fuImages);
if (mPath == "")
{
myShare.WebMessageBox(this.Page, "请选择图片");
return;
}
if (mPath == "eX")
{
myShare.WebMessageBox(this.Page, "图片上传失败!请重试");
return;
}
myShare.SetAField(0, "LogoUrl", "'" + mPath + "'", "Vendors", "VendorID=" + ViewState["VendorID"].ToString());
imgPreview.ImageUrl = mPath;
//myShare.WebMessageBox(this.Page, "图片上传成功");
}
private string uploadImage(FileUpload pControlName)
{
string str = "";
Random rd = new Random();
string mImageNewName = DateTime.Now.ToString("yyyyMMddHHmmssffff") + rd.Next(1000, 9999).ToString();//生成图片名
if (pControlName.PostedFile.ContentLength > 0)
{
//将图片切割为统一大小----start
HttpPostedFile mImg = pControlName.PostedFile;
System.IO.Stream mImgStr = mImg.InputStream;
System.Drawing.Image mImage = System.Drawing.Image.FromStream(mImgStr);
int mWidth = mImage.Width;
int mHeight = mImage.Height;
int mNWidth = 140, mNHeith = 50;
System.Drawing.Image mNImage = mImage.GetThumbnailImage(mNWidth, mNHeith, null, IntPtr.Zero);
//将图片切割为统一大小------end
int mStart = pControlName.PostedFile.FileName.LastIndexOfAny("\\".ToCharArray());
string mName = pControlName.PostedFile.FileName.Substring(mStart + 1);
string[] mStrName = mName.Split('.');
//上传至Ne下
string mPath = "images/TreeView/";
string mPhysicalPath = HttpContext.Current.Server.MapPath(mPath);
if (!Directory.Exists(mPhysicalPath))
Directory.CreateDirectory(mPhysicalPath);
string mPhysicalName = HttpContext.Current.Server.MapPath(mPath + mImageNewName + '.' + mStrName[1]);
//上传至Ne_Oversea下
string mOverseaPhysicalPath = mPhysicalPath.ToLower().Replace("Ne\\images", "Ne_Oversea\\PubImage");
if (!Directory.Exists(mOverseaPhysicalPath))
Directory.CreateDirectory(mOverseaPhysicalPath);
string mOverseaPhyiscalName = mOverseaPhysicalPath + "\\" + mImageNewName + '.' + mStrName[1];
try
{
pControlName.PostedFile.SaveAs(mPhysicalName);
pControlName.PostedFile.SaveAs(mOverseaPhyiscalName);
str = mPath + mImageNewName + '.' + mStrName[1];
}
catch (Exception ex)
{
str = "eX";
}
}
return str;
}