winform中上传图后怎么会这样啊
程序如下:
我明明上传成功了,但是到最后一个图片的话,程序就把最后一个图片删除了
protected void btnCommit_Click(object sender, EventArgs e)
{
bool isAllow = false;//the legality of uploaded file
//if (ddlCategory.SelectedIndex < 0) return;
///get the list of uploaded files
HttpFileCollection hpcFiles = HttpContext.Current.Request.Files;
if (hpcFiles == null)
{
lblMessage.Text = "bpcFiles is null";
}
photo p = new photo();
string fullPath = "";
try
{
lblMessage.Text = hpcFiles.Count.ToString();
///upload each file of the files list
for (int i = 0; i < hpcFiles.Count; i++)
{
HttpPostedFile file = hpcFiles[i];
if (file == null)
{ lblMessage.Text = "file is null"; continue; }
///get the file name
string filename = Path.GetFileNameWithoutExtension(file.FileName);
///get the file extension
string strExt = Path.GetExtension(file.FileName);
///get time-based string
string strFilename = photo.createDatetimeString(i);
///get the file size;
int size = file.ContentLength;
///get the file type
string strType = file.ContentType;
///to determine the legality of uploaded file
foreach (string ext in photo.ALLOWPHOTOFILELIST)
{
if (strExt == ext)
{
isAllow = true;
break;
}
}
string url = "photoes/" + strFilename + strExt;
fullPath = Server.MapPath(url);
if (isAllow == true)
{
file.SaveAs(fullPath);
p.AddPhoto(filename, url, strType, size, ddlCategory.SelectedValue,i);
lblMessage.Text = "添加成功";
}
}
Response.Redirect("category.aspx?categoryid=" + ddlCategory.SelectedValue);
}
catch (Exception ex)
{
lblMessage.Text = "上传失败:" + ex.Message;
//这个地方在程序都成功转到别的网页的时候,还是被执行了一下,为什么啊
if (File.Exists(fullPath) == true)
{
File.Delete(fullPath);
}
}
}