asp.net文件上传并显示文件名删除文件重新上传

发布时间:2020-06-24 12:57:09 作者:kt431128
来源:网络 阅读:707
          
前台页面:
<h3 class="mt20">活动/宣传信息</h3>
<table width="860"  class="info_tab3 ctabt" id="table2">
  <tr>
   <th width="15%">活动名称</th>
   <td><asp:TextBox ID="activityname"  size="78" runat="server"  TextName="活动名称" MaxLength="100" ></asp:TextBox><span class="title_red">*</span></td>
  </tr>
  <tr>
   <th>活动概要</th>
    <td>
  
    <asp:TextBox ID="summary"  Width="420px"  runat="server" Height="55"    TextMode="MultiLine"  onpropertychange="MaxLength(this,300)"></asp:TextBox></td>
    </tr>
  <tr>
    <th>活动时间段</th>
  
  <td> <asp:TextBox ID="time"  size="78" runat="server"  MaxLength="40" ></asp:TextBox></td>
  </tr>
  <tr>
    <th>活动地区</th>
    <td><asp:TextBox ID="area"   Width="420px"  runat="server" Height="55"   TextMode="MultiLine" onpropertychange="MaxLength(this,300)"></asp:TextBox></td>
  </tr>
  <tr>
    <th>活动地点</th>
   <td> <asp:TextBox ID="address"  size="78" runat="server" MaxLength="100" ></asp:TextBox></td>
  </tr>
  <tr>
    <th>宣传媒体</th>
   <td><asp:TextBox ID="media"  size="78" runat="server" MaxLength="100" ></asp:TextBox></td>
  </tr>
</table>
 
<h3 class="mt20">上传附件</h3>
<tr align="left">
<div class="boperation"  >
<center>
&nbsp;<asp:UpdatePanel ID="UpdatePanel1" runat="server" RenderMode="Block" UpdateMode="Conditional">
        <ContentTemplate>
            其他格式促销资料:<asp:FileUpload ID="fuPhoto" runat="server" onchange="PreviewImg();"
                Width="300px" />
        </ContentTemplate>
 <Triggers>
 <asp:PostBackTrigger ControlID="delA" />
 </Triggers>
</asp:UpdatePanel>
<asp:HyperLink ID="filePath" runat="server" Target="_blank" Visible="false" ></asp:HyperLink>
<asp:LinkButton Visible="false" ID="delA" Text="[X]" runat="server" OnCommand="delA_Click"></asp:LinkButton>
<asp:HiddenField ID="fileUploadPath" runat="server" />
 
 <asp:UpdatePanel ID="UpdatePanelFileUpload" runat="server" RenderMode="Block" UpdateMode="Conditional">
 <Triggers>
 <asp:PostBackTrigger ControlID="lbUploadPhoto" />
 </Triggers>
</asp:UpdatePanel>
<asp:LinkButton ID="lbUploadPhoto" runat="server" Text="aa" OnClick="lbUpload_Click"></asp:LinkButton>
</center>
  </div>
 
 
后台处理:
 //上传附件
        protected void lbUpload_Click(object sender, EventArgs e)
        {
            fileUpload();
        }
        //删除附件
        protected void delA_Click(object sender, CommandEventArgs e)
        {
            string path = fileUploadPath.Value;
            path = Server.MapPath(path);
            FileInfo file = new FileInfo(path);
            if (file.Exists)
            {
                file.Delete();
            }
            fileUploadPath.Value = "";
            fuPhoto.Visible = true;
            filePath.Visible = false;
            delA.Visible = false;
        }
        public void fileUpload()
        {
            if (fuPhoto.PostedFile != null && fuPhoto.PostedFile.ContentLength > 0)
            {
                string ext = System.IO.Path.GetExtension(fuPhoto.PostedFile.FileName).ToLower();
                //if (ext != ".jpg" && ext != ".jepg" && ext != ".bmp" && ext != ".gif" && ext != ".png")
                //{
                //    this.Alert("请上传(*.jpg,*.jepg,*.bmp,*.gif,*.png)格式的图片!");
                //    return;
                //}
                string dir = Server.MapPath(PRODUCE_TEMP_FILE_PATH);
                if (Directory.Exists(dir) == false)
                {
                    Directory.CreateDirectory(dir);
                }
                string filename = DateTime.Now.ToString("yyyyMMddHHmmssffff") + ext;
                string path = PRODUCE_TEMP_FILE_PATH + filename;
                fuPhoto.PostedFile.SaveAs(Server.MapPath(path));
                fileUploadPath.Value = path;
                fuPhoto.Visible = false;
                delA.Visible = true;
                filePath.Visible = true;
                filePath.Text = fuPhoto.FileName;
                filePath.NavigateUrl = path;
                // uploadfilepath.Text = path;
            }
            else
            {
                //do some thing;
            }
        }
 
 
保存

            ProductSellPlanEntity sellplan = new ProductSellPlanEntity();
            sellplan.ResourceId = System.Guid.NewGuid().ToString();
            sellplan.ProjectId = productNum.Text;
            sellplan.CreateNumber = createNumber.Text;
            sellplan.ContantCompany = companyID.Value;
            sellplan.ProjectTime = projecttime.Text;
            sellplan.CreateUser = this.CurrentUser.UserName;
            sellplan.CreateUserID = CurrentUser.UserId;
            sellplan.Telphone = CurrentUser.OfficePhone;
            sellplan.ActivityName = activityname.Text;
            sellplan.ActivityDescription = summary.Text;
            sellplan.ActivityTime = time.Text;
            sellplan.ActivityArea = area.Text;
            sellplan.ActivityAddress = address.Text;
            sellplan.PropagandaMedia = media.Text;
            sellplan.UploadFilename = filePath.Text;
            if (!string.IsNullOrEmpty(fileUploadPath.Value))
            {
                string path = Server.MapPath(fileUploadPath.Value);
                FileInfo file = new FileInfo(path);
                if (file.Exists)
                {
                    string dir = Server.MapPath(PRODUCE_FILE_PATH);
                    if (Directory.Exists(dir) == false)
                    {
                        Directory.CreateDirectory(dir);
                    }
                    string fname = DateTime.Now.ToString("yyyyMMddHHmmssffff") + filePath.Text;
                    string targetFile = dir + fname;
                    if (path.IndexOf("ImportFiles") >= 0)
                    {
                        file.CopyTo(targetFile);
                    }
                    sellplan.UploadFilepath = PRODUCE_FILE_PATH + fname;
                }
                if (path.IndexOf("ImportFiles") >= 0)
                {
                    FileInfo filetemp = new FileInfo(path);
                    filetemp.Delete();
                }
            }
 
 
 
效果图片:asp.net文件上传并显示文件名删除文件重新上传
推荐阅读:
  1. form里面文件上传并预览
  2. Asp.net UploadFile 文件上传

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

职场 asp.net 休闲

上一篇:php文件下载的原理

下一篇:Webpack4教程 - 第三部分,如何使用插件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》