复制代码 代码如下:
<div>
<div>
<input type="file" size="50" />
<span></span>
<br />
<input type="button" value="添加文件">
<input type="button" value="删除文件">
</div>
<div>
<asp:Button runat="server" Text="上传"></asp:Button><br/>
<asp:Label runat="server"></asp:Label>
</div>
</div>
复制代码 代码如下:
<script type="text/javascript">
var attachname = "uploadfile";
var i = 1;
function addInput() {
if (i > 0) {
var attach = attachname + i;
if (createInput(attach))
i = i + 1;
}
}
function deleteInput() {
if (i > 1) {
i = i - 1;
if (!removeInput())
i = i + 1;
}
}
function createInput(nm) {
var aElement = document.createElement("input");
aElement.name = nm;
aElement.id = nm;
aElement.type = "file";
aElement.size = "50";
if (document.getElementById("upload").appendChild(aElement) == null)
return false;
return true;
}
function removeInput(nm) {
var aElement = document.getElementById("upload");
if (aElement.removeChild(aElement.lastChild) == null)
return false;
return true;
}
</script>
复制代码 代码如下:
protected void btnUpload_Click(object sender, EventArgs e)
{
//遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
System.Text.StringBuilder strMsg = new StringBuilder("<br/>");
strMsg.Append("上传的文件分别是:</br>");
try
{
for (int iFile = 0; iFile < files.Count; iFile++)
{
//检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
fileExtension = System.IO.Path.GetExtension(fileName);
strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br/>");
strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br/>");
strMsg.Append("上传文件的文件名:" + fileName + "<br/>");
strMsg.Append("上传文件的扩展名:" + fileExtension + "<br/>");
strMsg.Append("上传文件的大小:" + postedFile.ContentLength + "<br/>");
//可扩展功能:
//保存文件时可以设置保存目录
//可以重命名文件保存
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("images/") + fileName);
}
}
strStatus.Text = strMsg.ToString();
}
catch (System.Exception Ex)
{
strStatus.Text = Ex.Message;
}
}
新闻热点
疑难解答
图片精选