个人文件上传asp.net c#
当我点击链接按钮添加更多文件上传时,这只添加了1。。表示1个已可见的文件上载和1个单击链接按钮显示的文件上载 但当我再次单击linkbutton添加更多内容时,将不会显示 我试试这个 好的,我做这个个人文件上传asp.net c#,c#,C#,当我点击链接按钮添加更多文件上传时,这只添加了1。。表示1个已可见的文件上载和1个单击链接按钮显示的文件上载 但当我再次单击linkbutton添加更多内容时,将不会显示 我试试这个 好的,我做这个 <form id="form1" runat="server"> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:LinkButton ID="LinkButton1" runat="se
<form id="form1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">upload</asp:LinkButton><br />
<asp:Panel ID="Panel1" runat="server"></asp:Panel>
<asp:Button ID="Button1" runat="server" Text="submit" OnClick="Button1_Click" />
</form>
上传
假设有文件上载控件。。当我们通过单击浏览选择文件,然后单击上载,则应显示扩展名为abc.doc的文件名。。。然后,当我们再次使用相同的文件上载并单击浏览并选择另一个文件时,将显示该文件,即xyz.pdf。。所以有两个文件abc.doc和xyz.doc。。这就是我想要做的。。我想上传文件并显示这些上传的文件
更新
为了这个,我试试这个
protected void LinkButton1_Click(object sender, EventArgs e)
{
fileuploadd(FileUpload1.FileName);
}
public void fileuploadd(string filename)
{
try
{
HttpFileCollection hfc = Request.Files;
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > 0)
{
SMSEntities s = new SMSEntities();
uploaded_file u = new uploaded_file();
{
u.fileupload = filename;
}
s.uploaded_file.Add(u);
s.SaveChanges();
}
//hpf.SaveAs(Server.MapPath("upload") + "\\" + System.IO.Path.GetFileName(hpf.FileName));
Response.Write("<b>File: </b>" + hpf.FileName + " <b>Size:</b> " + hpf.ContentLength + " <b>Type:</b> " + hpf.ContentType + " Uploaded Successfully <br/>");
}
}
catch (DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name, eve.Entry.State);
foreach (var ve in eve.ValidationErrors)
{
Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName, ve.ErrorMessage);
}
}
throw;
}
}
受保护的无效链接按钮1\u单击(对象发送者,事件参数e)
{
fileuploadd(FileUpload1.FileName);
}
已上载公共无效文件(字符串文件名)
{
尝试
{
HttpFileCollection hfc=Request.Files;
对于(int i=0;i0)
{
SMsenties s=新的SMsenties();
上传的_文件u=新上传的_文件();
{
u、 fileupload=文件名;
}
s、 上传的_文件。添加(u);
s、 保存更改();
}
//SaveAs(Server.MapPath(“upload”)+“\\”+System.IO.Path.GetFileName(hpf.FileName));
Response.Write(“文件:+hpf.FileName+”大小:+hpf.ContentLength+”类型:+hpf.ContentType+”上传成功
);
}
}
捕获(DbEntityValidationException e)
{
foreach(e.EntityValidationErrors中的变量eve)
{
WriteLine(“状态为“{1}\”的类型为“{0}\”的实体”存在以下验证错误:“,
eve.Entry.Entity.GetType().Name,eve.Entry.State);
foreach(eve.ValidationErrors中的变量ve)
{
Console.WriteLine(“-Property:\“{0}\”,错误:\“{1}\”,
ve.PropertyName,ve.ErrorMessage);
}
}
投掷;
}
}
所以当我选择文件并点击上传时,文件名就显示出来了。。但当我再次选择文件并单击上载时,第一个文件名消失,第二个文件名显示在我想要的位置
所以我如何显示这两个文件名。。。当点击submit时,记录文件应该分别插入到表中。我已经完成了,但不能同时显示两个文件名 在.aspx上创建文件上载控件,如:
<asp:FileUpload runat="server" ID="UploadImages" AllowMultiple="true" />
然后在代码隐藏中:
protected void uploadFile_Click(object sender, EventArgs e)
{
if (UploadImages.HasFiles)
{
foreach (HttpPostedFile uploadedFile in UploadImages.PostedFiles)
{
uploadedFile.SaveAs(System.IO.Path.Combine(Server.MapPath("~/Images/"),
uploadedFile.FileName)); listofuploadedfiles.Text += String.Format("{0}<br />", uploadedFile.FileName);
}
}
}
受保护的无效上载文件\u单击(对象发送方,事件参数e)
{
if(上传images.HasFiles)
{
foreach(UploadImages.PostedFiles中的HttpPostedFile uploadedFile)
{
uploadedFile.SaveAs(System.IO.Path.Combine(Server.MapPath(“~/Images/”)),
uploadedFile.FileName);listofUploadedFile.Text+=String.Format(“{0}
”,uploadedFile.FileName);
}
}
}
嗨超级用户以前我也有同样的问题。
我实际上做的是创建了5个文件控件
1) 文件上传1
2) 文件上传2
3) 文件上传3
4) 文件上传4
5) 文件上传5
我有四个是隐藏的。
点击第一个我显示第二个,点击第二个我显示第三个,依此类推
在控制器/方法中,我检查文件控件是否为null,然后通过获取文件上传数组继续正常的文件上传过程
<div id="flOther" runat="server">
<div id="fileOtherUploadarea">
<asp:FileUpload ID="flOtherUPL" runat="server" />
</div>
<input style="width: 20px; border: 0px none; background-color:transparent;" id="btnOtherAddMoreFiles" type="button" onclick="AddOtherMoreImages();" class="icon-plus-sign" />|
<asp:LinkButton ID="lnkOtherUpload" OnCommand="btnLnk_UploadOtherFiles" CommandArgument='<%# Eval("ID") %> 'runat="server">Upload Files</asp:LinkButton>
</div>
<script language="javascript" type="text/javascript">
function AddMoreImages() {
if (!document.getElementById && !document.createElement)
return false;
var fileUploadarea = document.getElementById("fileUploadarea");
if (!fileUploadarea)
return false;
var newLine = document.createElement("br");
fileUploadarea.appendChild(newLine);
var newFile = document.createElement("input");
newFile.type = "file";
newFile.setAttribute("class", "fileUpload");
if (!AddMoreImages.lastAssignedId)
AddMoreImages.lastAssignedId = 100;
newFile.setAttribute("id", "FileUpload" + AddMoreImages.lastAssignedId);
newFile.setAttribute("name", "FileUpload" + AddMoreImages.lastAssignedId);
var div = document.createElement("div");
div.appendChild(newFile);
div.setAttribute("id", "div" + AddMoreImages.lastAssignedId);
fileUploadarea.appendChild(div);
AddMoreImages.lastAssignedId++; }
</script>
protected void btnLnk_UploadOtherFiles(object sender, CommandEventArgs e)
{
int ID;
try
{
if (!String.IsNullOrEmpty(Convert.ToString(e.CommandArgument)))
{
ID = Convert.ToInt32(e.CommandArgument);
HttpFileCollection hfc = Request.Files;
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
if (hpf.ContentLength > 0)
{
string fileExtension = System.IO.Path.GetExtension(hpf.FileName);
if (ValidExtesion(fileExtension))
{
int MaxSizeAllowed = Convert.ToInt32(ConfigurationManager.AppSettings["MaxFileSize"]);
if (hpf.ContentLength < MaxSizeAllowed)
{
int lastIndex = Convert.ToInt32(hpf.FileName.LastIndexOf("\\"));
string FileName = DateTime.Now.Millisecond + hpf.FileName.Substring(lastIndex + 1);
string FileName1 = hpf.FileName.Substring(lastIndex + 1);
FileName = FileName.Replace(" ", "");
StringBuilder AncTag = new StringBuilder();
AncTag = AncTag.Append("<a href='Attachments/AMT/'" + FileName + "' target='_blank'>'" + FileName1 + "' </a>");
string strAncTag = AncTag.ToString();
strAncTag = strAncTag.Replace("'", "");
hpf.SaveAs(AppDomain.CurrentDomain.BaseDirectory + "Attachments/AMT/" + FileName);
obj.UploadOtherFiles_ProblemDescription(ID, strAncTag);
}
else
{
}
}
else
{
}
}
}
}
}
catch (Exception ex)
{
ErrorLog objER = new ErrorLog(ex);
}
finally
{
obj = null;
grdPDC.EditIndex = -1;
}
}
");
string Strantag=AncTag.ToString();
strAncTag=strAncTag.Replace(“,”);
hpf.SaveAs(AppDomain.CurrentDomain.BaseDirectory+“Attachments/AMT/”+文件名);
对象上载其他文件\u问题描述(ID,strAncTag);
}
其他的
{
}
}
其他的
{
}
}
}
}
}
捕获(例外情况除外)
{
ErrorLog objER=新的ErrorLog(ex);
}
最后
{
obj=null;
grdPDC.EditIndex=-1;
}
}
这不起作用..我想单独上传文件.,…我不想一次上传多个文件.那么你的问题是错的,title说多个文件上传,你到底有什么问题?@superuser所以基本上你想动态创建更多的文件上传控制?你能更清楚地解释你的想法吗ppose有文件上传控制。当我们通过点击浏览选择文件,然后点击上传,然后显示扩展名为abc.doc的文件名。然后当我们再次使用相同的文件上传,然后点击浏览选择另一个文件,然后文件将显示,即xyz.pdf。所以有两个文件abc.doc和xyz.doc。所以这是我想上传文件并显示这些上传的文件files@superuser哦,我明白了,它会在第一个文件插入fileupload后回发吗?