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"] 的数据并且插入到数据库,即记录多张多媒体文件的。

你可能感兴趣的:(fileupload)