附件上传 使用javascript

时间:2023-03-08 17:24:52
附件上传 使用javascript
 <html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script type="text/javascript">
function ShowImg(obj) {
var a = obj.toString();
var isNumber = /^[-]$/;
var img = "";
if (isNumber.test(a)) {
img = a;
a = "div_" + a;
}
var docObj = document.getElementById(a);
var img1 = document.getElementById("img_"+img);
if (docObj.files && docObj.files[]) {
//火狐7以上版本不能用上面的getAsDataURL()方式获取,需要一下方式
img1.src = window.URL.createObjectURL(docObj.files[]);
document.getElementById("sname").value += img1.src+" ";
}
return true;
}
//设置ID变量参数
var fileId = ;
var fileId_ = ;
//添加附件 生成一个DIV 里面包含file控件和图片
function addFile() {
//获取div(包含file控件和图片)
var FilesDiv = document.getElementById('FilesDiv');
//设置一个DIV的id为“div1”
var divId = "div" + fileId;
//拼接一个div 让其ID自增长 可一直添加
var str = '<div id="' + divId + '">';
str += '<img id="img_' + fileId + '" src="" style=" width:100px; height:100px" /><br />'
str += '<input type="file" size="40" id="div_' + fileId + '" onchange="ShowImg(\'' + fileId + '\')" name="File" style="border: solid 1px #0077B2">'
str += '&nbsp;<img src="../images/icon_stop.gif" onclick="delFile(\'' + divId + '\')"/>';
str += "<div>";
FilesDiv.insertAdjacentHTML("beforeEnd", str)
fileId++;
}
function delFile(obj) {
var FilesDiv = document.getElementById('FilesDiv');
var elem = document.getElementById(obj);
FilesDiv.removeChild(elem);
}
function Sub() {
var len = document.getElementById('FilesDiv').getElementsByTagName('input');
if (len.length != ) {
for (var i = ; i < len.length; i++) {
if (len[i].type == "file") {
if (len[i].value == "") {
alert('请选择要上传的附件!');
return false;
}
else { }
}
}
}
else {
alert('请选择要上传的附件!');
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="FilesDiv">
<div id="div0">
<img id="img_0" src="" style=" width:100px; height:100px" /><br />
<input type="file" runat="server" size="" name="File" id="file0" onchange="ShowImg('file0')" style="border: solid 1px #0077B2" />&nbsp;
<img src="../images/icon_stop.gif" onclick="delFile('div0')" />
</div>
</div>
<input type="button" value="添加附件" onclick="addFile();return false" id="btnInput" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="上传附件" OnClick="btnUpload_Click" OnClientClick="return Sub();"/><P>
</P>
<input type="hidden" name="sname" id="sname" />
</div>
</form>
</body>
</html>

后台

  protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{ }
}
/// 检测指定的文件夹是否存在,不存在就创建
/// </summary>
/// <param name="imgpath">该文件夹的之前的路径,注意一定要带上"/"</param>
/// <returns></returns>
public static string CheckFile()
{
//命名一个今天的文件夹
string folder = DateTime.Now.ToString("yyyyMMdd"); //判断文件是否存在
if (!System.IO.Directory.Exists(folder))
{
//自动生成文件夹
System.IO.Directory.CreateDirectory(folder); //生成后返回文件夹名
return folder;
} //如果存在,直接返回今天的文件夹名
return folder;
} protected void btnUpload_Click(object sender, EventArgs e)
{
if (Request.Form["sname"] != null)
{
//获取到附件存放的URL
string UrlName = Request.Form["sname"];
string[] strArr = UrlName.Split(' ');
}
//声明名称、大小、类型等几个变量
string f_name, f_size, f_tye;
HttpFileCollection hfc = Request.Files;
for (int i = ; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
f_name = hpf.FileName;
f_size = hpf.ContentLength.ToString();
f_tye = hpf.ContentType; } }

今天写到这。。。。。