zoukankan      html  css  js  c++  java
  • asp.net实现图片上传

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            
    if (txtName.Text == "")
            {
                Tools.Alert(
    "填写图片名称!"this);
                
    return;
            }
            
    string savePath = @"../images/picnews/";//图片保存路径
            string fileName = Server.HtmlEncode(this.getimagepath());
            
    string extension = System.IO.Path.GetExtension(fileName);//取得扩展名
            string ImageName = "";
            
    if (fileName != "nopic.gif")
            {
                
    if ((extension == ".jpg"| (extension == ".gif"))
                {
                    ImageName 
    = DateTime.Now.ToString("yyyyMMddhhmmssfff"+ extension;
                    savePath 
    += ImageName;
                    savePath 
    = Server.MapPath(savePath);
                }
                
    else
                {
                    Response.Write(
    "<script>alert('文件上传格式错误');history.back(1);</script>");
                    Response.End();
                }
            }
            
    else
            {
                ImageName 
    = fileName;
            }
            
    if (fileName != "nopic.gif")
            {
                picupload.SaveAs(savePath);
    //这里是asp:FileUpload控件
            }
            AProduct.AddProduct(txtName.Text, FreeTextBox1.Text, ImageName, Convert.ToInt32(ddlType.SelectedValue), FreeTextBox2.Text);
            Tools.Alert(
    "添加成功!"this);
        }
        
    /// <summary>
        
    /// 当没有选择图片时,使用默认图片。为了保存在数据库中。
        
    /// </summary>
        
    /// <returns></returns>
        private string getimagepath()
        {
            
    try
            {
                
    return this.picupload.HasFile ? picupload.FileName : "nopic.gif";
            }
            
    catch
            {
                
    return "nopic.gif";
            }
        }
  • 相关阅读:
    [BZOJ]2589: Spoj 10707 Count on a tree II
    [BZOJ]2434: [Noi2011]阿狸的打字机
    Codeforces Round #408 (Div. 2)
    [BZOJ]2653: middle
    洛谷4月月赛R1
    2017省夏令营Day8
    2017省夏令营Day7
    2017省夏令营Day6
    【20170604校内模拟赛】香蕉
    【20170602模拟赛】秋之国的夏日祭
  • 原文地址:https://www.cnblogs.com/greatverve/p/1564176.html
Copyright © 2011-2022 走看看