.aspx:
<tr>
<th>附件上传</th>
<td colspan="5">
<input type="hidden" id="hdAttachIds" runat="server"/>
<asp:Repeater ID="rpAttachment" runat="server" OnItemCommand="rpAttachment_ItemCommand">
<HeaderTemplate>
<table class="table-list">
<thead><tr><th>序号</th><th>附件名称</th><th>附件大小</th><th>操作</th></tr></thead>
</table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td><a href='<%#ResolveUrl(Eval("FilePath").ToString())%>' target="_blank"><%#Eval("FileName")%></a></td>
<td><%#Eval("FileSize")%> KB</td>
<td><asp:LinkButton ID="btnDelete" Text="删除附件" CommandName="Delete" CommandArgument='<%#Eval("Id")%>' runat="server" /></td>
</tr>
</ItemTemplate>
</Repeater>
<asp:FileUpload ID="fileUploadAttachment" runat="server" cssClass="input-text" />
<asp:linkbutton id="btnUploadAttach" onClientclick="showLoading();" Onclick="btnUploadAttach_Click"><i>上传附件</i></asp:linkbutton>
</td>
</tr>
aspx.cs:
protected void btnUploadAttach_Click(object sender,EventArgs e)
{
if(fileUploadAttachment.HasFile)
{
string filename=fileUploadAttachment.FileName;
string path="~/Upload/Proposal/"+Datatime.Now.Year+"/" + DateTime.Now.Ticks + Path.GetExtension(fileName);
try{
if(!FileHelper.CheckAttachmentExtension(fileName))//检测是否符合上传的文件名的规范
waring("上传文件类型非法!");
if(!FileHelper.UploadFile(fileUploadAttachment.PostedFile,Server.MapPath(Path)))
warning("上传失败,请联系管理员!")//路径不正确
var fileRecord=new FileRecord{
FileName=fileName,
FilePath=path,
FileSize=string.Format("{0:#.##}", fileUploadAttachment.PostedFile.ContentLength / 1024.0);
Inputer=CurrentUser.RealName,
InputTime=DateTime.Now,
};
FileRecordService.Add(FileRecord);
FileRecordIds+fileRecord.id+",";//存储修改信息包含的附件id列表
LoadAttachment();//重新加载页面
}
}
catch(Exception err){
Warning("上传附件失败:"+HttpUtility.HtmlEncode(err.Message))
}
}