• 文件上传(实例)


    .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")%>&nbsp;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))

      }

    }

  • 相关阅读:
    iTerm2分屏快捷键
    k8s中运维/测试常用的命令整理(随时更新)
    httpRunner自动化测试用例使用笔记
    Git学习笔记-快速上手(mac系统)
    RBAC权限控制逻辑笔记
    CPS中有关CICD的配置
    LDAP中filter的使用
    Docker初级入门
    C语言 实现 HashTable
    从三个线程 排队打印 , 到 多个线程打印
  • 原文地址:https://www.cnblogs.com/sunzgod/p/4344451.html
Copyright © 2020-2023  润新知