FileUpload上传多张图片(多媒体) ---.net的应用

  protected void btnUpload_Click(object sender, EventArgs e)
    {string type;
        if (ddlist.SelectedItem.Value == "")
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('你还没有选择专栏');window.location.href='User.aspx';", true);
          
            return;
        }
        else
        {
            type = ddlist.SelectedItem.Value;
            ViewState["type"] = type;
            ddlist.Enabled = false;
        }
        Boolean fileOK = true;
        if (fUfile.HasFile)
        {
            if (fileOK)
            {
                try
                {
                    string Date = System.DateTime.Now.ToString("yyMMddhhmmss");
                    string subFie = fUfile.FileName.Substring(fUfile.FileName.LastIndexOf("."), 4);
                    string sLocalPath = "../common/wwjeditor/UploadFiles/";
                    string folder = Server.MapPath(sLocalPath)  + type;

                    if (!Directory.Exists(folder))
                        Directory.CreateDirectory(folder);
                  
                    string path = folder + "//" + Date + subFie;

                    string fileName = fUfile.FileName;
                    int size = fUfile.PostedFile.ContentLength;
                   // string userName = type;

                    Multimedia mul = new Multimedia();
                    mul.Name = fileName;
                    mul.Size = size;
                    mul.Path = "common/wwjeditor/UploadFiles/" + type + "/" + Date + subFie;
                    mul.Series = 1;

 

                    //上传到文件夹
                    sLocalPath = "../common/wwjeditor/UploadFiles/" + type;
                    path = Server.MapPath(sLocalPath) + "//" + mul.Path.Substring(mul.Path.LastIndexOf("/") + 1);
                    if (!File.Exists(path))
                    {
                        fUfile.SaveAs(path);
                    }
    

                    if (ViewState["list"] != null)
                    {
                        List<Multimedia> list =(List<Multimedia>) ViewState["list"];
                        mul.Series = list.Count + 1;
                        list.Add(mul);
                        gvAccessory.DataSource = list;
                        gvAccessory.DataBind();
                    }
                    else
                    {
                        List<Multimedia> list = new List<Multimedia>();
                        list.Add(mul);
                        ViewState["list"] = list;
                        gvAccessory.DataSource = list;
                        gvAccessory.DataBind();
                    }

                }
                catch (System.Exception ex)
                {
                    Response.Write("<script>alert('" + ex.Message + "');</script>");
                    return;
                }
            }
        }
        else
        {
            Response.Write("<script>alert('上传的文件不符合要求!');</script>");
            return;
        }

}

 

用此类方法可以上传多张图片,或多张多媒体文件。然后点击确定保存的时候,就可以把数据插入到数据库里面,读取ViewState["list"] 的数据并且插入到数据库,即记录多张多媒体文件的。

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值