求教,单页面上传附件问题与讨论。
在vs2005中,关于用户上传附件的操作,我想用ajax.net操作,但是不能实现多次上传,我用的是刷新的方式做的,
不知道现在大家有什么好的解决方案,一起讨论一下:
要求:
1.一张页面内有8个要求上传附件的选项。
2.每个上传附件的选项,最多可以上传10项内容。
3.附件内容形式是图片和文本。
4.上传的附件可以删除(最好可以修改)。
需要附件的页面是一张大的表单,有60左右字段,一方面要考虑界面友好交互,另一方面也要考虑与数据库的交互情况。
根据以上要求,我用比较笨的方法可以基本实现功能,功能实现了,我想考虑怎么能有更好的方法解决这个问题。
下面是我笨拙的代码,希望给点建议。
protected void lbtnOupload1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(FileUpload1.FileName))
{
int i = (int)ViewState["count1"];
if (i == 4)//记录附件个数
{
lbtnOupload1.Visible = false;
FileUpload1.Visible = false;
}
string LastName = "";
string FileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Second.ToString();
string fileurl = common.DoFileUpload(FileUpload1, Server.MapPath("attFile"), FileName, out LastName);//调用上传方法,上传图片。
if (LastName == "gif" || LastName == "jpg" || LastName == "jpeg")//判断附件类型
{//图片类
ViewState["upload1"] += "<a href=attFile/" + fileurl + " target='_blank'><img src=attFile/" + fileurl + " width='60' height='60'/></a>,";
lblupload1.Text = ViewState["upload1"].ToString();//显示图片的串进行累加,显示所有上传的图片
ViewState["lbluplaod1"] += fileurl + ",";//记录图片的路径,插入到数据库中
}
else
{//文本类
ViewState["upload1"] += "<a href=attFile/" + fileurl + " >" + fileurl + "</a>,";
lblupload1.Text = ViewState["upload1"].ToString();
ViewState["lbluplaod1"] += fileurl + ",";
}
ViewState["count1"] = (int)ViewState["count1"] + 1; //记录附件个数
}
}
protected void lbtnUndo2_Click(object sender, EventArgs e)
{
ViewState["count1"] = (int)ViewState["count1"] - 1; //设置获取上传文件的个数
string UploadStr = lblupload1.Text.Remove(lblupload1.Text.Length - 1);//UploadStr是上传后记录图片的字符串
string RemoveFileUrl = null;//要删除的文件字符串
int intLastIndexOf = UploadStr.LastIndexOf(",") == -1 ? 0 : UploadStr.LastIndexOf(",");
int count = UploadStr.Substring(intLastIndexOf).Length;
string UploadFileUrl = (string)ViewState["lbluplaod1"];//设置上传的文件的路径
UploadFileUrl = UploadFileUrl.Remove(UploadFileUrl.Length - 1);//删除上传图片的路径
int FileUrl = UploadFileUrl.Substring(UploadFileUrl.LastIndexOf(",") == -1 ? 0 : UploadFileUrl.LastIndexOf(",")).Length;//获取上传附件个数
RemoveFileUrl = UploadFileUrl.Substring(UploadFileUrl.LastIndexOf(",") == -1 ? 0 : UploadFileUrl.LastIndexOf(","));//要删除的当前附件字符串
lblupload1.Text = UploadStr.Remove(UploadStr.Length - count) + ",";//从字符串中删除当前的字符串
ViewState["upload1"] = UploadStr.Remove(UploadStr.Length - count) + ",";附件个数-1
ViewState["lbluplaod1"] = UploadFileUrl.Remove(UploadFileUrl.Length - FileUrl) + ",";
Artworks artWorks = new Artworks();//执行数据库操作
if (RemoveFileUrl.Remove(RemoveFileUrl.Length - 1) != "&q