Javascript 使用FileUpload上载文件后使链接可见
我正在使用FileUpload上传文件。文件上传后,我希望用户看到一个“删除”链接,该链接将从服务器上删除该文件Javascript 使用FileUpload上载文件后使链接可见,javascript,asp.net,Javascript,Asp.net,我正在使用FileUpload上传文件。文件上传后,我希望用户看到一个“删除”链接,该链接将从服务器上删除该文件 我如何使这样的链接在文件完全上传后才可见?我已经尝试使用数据控件来实现这一点 <asp:Panel ID="pnlUpload" runat="server"> <asp:FileUpload ID="fup1" runat="server" ClientIDMode="Static" /> <asp:Button ID="btnSer
我如何使这样的链接在文件完全上传后才可见?我已经尝试使用数据控件来实现这一点
<asp:Panel ID="pnlUpload" runat="server">
<asp:FileUpload ID="fup1" runat="server" ClientIDMode="Static" />
<asp:Button ID="btnServer" runat="server" Text="Submit" OnClick="btnSubmit_Click"
ClientIDMode="Static" CssClass="none" />
</asp:Panel>
<asp:Repeater ID="rptUploads" runat="server" OnItemCommand="rptUploads_ItemCommand">
<HeaderTemplate>
<table width="100%">
<th>
File Name
</th>
<th>
Actions
</th>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Container.DataItem %>
</td>
<td>
<asp:LinkButton ID="LinkButton1" runat="server" Text="Remove" CommandName="Delete"
CommandArgument="<%# Container.DataItem %>"></asp:LinkButton>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<style>
.none
{
display: none;
}
</style>
<script type="text/javascript">
(function () {
if (document.getElementById('fup1') != null) {
document.getElementById('fup1').onchange = function () { document.getElementById('btnServer').click(); };
}
})();
</script>
文件名
行动
没有一个
{
显示:无;
}
(功能(){
if(document.getElementById('fup1')!=null){
document.getElementById('fup1')。onchange=function(){document.getElementById('btnServer')。单击();};
}
})();
和代码隐藏
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
public List<string> Files
{
get
{
if (ViewState["files"] == null)
{
ViewState["files"] = new List<string>();
}
return (List<string>)ViewState["files"];
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (fup1.HasFile && !Files.Contains(fup1.FileName))
{
fup1.SaveAs(Server.MapPath("~/App_Data/") + fup1.FileName);
Files.Add(fup1.FileName);
rptUploads.Visible = true;
rptUploads.DataSource = Files;
rptUploads.DataBind();
pnlUpload.Visible = false;
}
}
protected void rptUploads_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
var filename = Server.MapPath("~/App_Data/") + e.CommandArgument;
if (File.Exists(filename))
{
File.Delete(filename);
Files.Remove(e.CommandArgument.ToString());
rptUploads.Visible = false;
pnlUpload.Visible = true;
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.IO;
公共部分类\u默认值:System.Web.UI.Page
{
公共列表文件
{
得到
{
if(ViewState[“files”]==null)
{
ViewState[“文件”]=新列表();
}
返回(列表)视图状态[“文件”];
}
}
受保护的无效页面加载(对象发送方、事件参数e)
{
}
受保护的void btnsupmit\u单击(对象发送者,事件参数e)
{
if(fup1.HasFile&&!Files.Contains(fup1.FileName))
{
fup1.SaveAs(Server.MapPath(“~/App\u Data/”)+fup1.FileName);
Files.Add(fup1.FileName);
rptUploads.Visible=true;
rptUploads.DataSource=文件;
rptouploads.DataBind();
pnlUpload.Visible=false;
}
}
受保护的void rptUploads_ItemCommand(对象发送器、RepeaterCommandEventArgs e)
{
如果(如CommandName==“删除”)
{
var filename=Server.MapPath(“~/App\u Data/”)+e.CommandArgument;
if(File.Exists(filename))
{
删除(文件名);
删除(例如CommandArgument.ToString());
rptUploads.Visible=false;
pnlUpload.Visible=true;
}
}
}
}
非常感谢,我想用fileupload添加一个文件。你的意思是,对于一个文件,使用数据控件会太多?仅供参考,我也尝试过使用动态控件,但代码比这更笨拙,所以首选这种方式…我想,如果fileupload有文件,用户不添加文件。意思是:当选择文件时,文件上载被禁用。请检查更新的代码。将上传控件包装到面板中,并设置visible=false。非常感谢,但有一个问题:如果不单击submit按钮,是否无法理解文件上传已完成??!!!