图片上传 显示缩略图_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > 图片上传 显示缩略图

图片上传 显示缩略图

 2013/11/7 19:16:42  porray  博客园  我要评论(0)
  • 摘要:<td><asp:FileUploadID="fuImages"runat="server"/><asp:ButtonID="btnUpload"runat="server"Text="上传"OnClick="btnUpload_Click"/><asp:ImageID="imgPreview"runat="server"Height="50"Width="140"AlternateText="暂无图片"/></td>
  • 标签:图片 上传 图片上传
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;
}
上一篇: Java里静态与非静态该什么情况下使用 下一篇: 没有下一篇了!
发表评论
用户名: 匿名