以下是具体代码:BasePage中
/// <summary>
/// 获取控件的位置,然后设置点击控件的位置,用于input type=file 上
/// </summary>
public void IEGetCtrlPlace()
{
string sHTML = "";
sHTML += "<script language=\"javascript\">\n";
sHTML += "function fileAttachmentclick(obj)\n";
sHTML += "{\n";
sHTML += " with(obj){\n";
sHTML += "style.posTop=event.srcElement.getBoundingClientRect().top\n";
sHTML += " var x=event.x-offsetWidth/2\n";
sHTML += " if(x<event.srcElement.getBoundingClientRect().left)x=event.srcElement.getBoundingClientRect().left\n";
sHTML += "if(x>event.srcElement.getBoundingClientRect().left+event.srcElement.offsetWidth-offsetWidth)x=event.srcElement.getBoundingClientRect().left+event.srcElement.offsetWidth-offsetWidth\n";
sHTML += "style.posLeft=x\n";
sHTML += "}\n";
sHTML += "}\n";
sHTML += "</script>\n";
Page.RegisterStartupScript("IEGetCtrlPlace", sHTML);
}
注:在网上那段控制位置的script定位有问题,它用的是event.srcElement.offsetTop与event.srcElement.offsetLeft. /// 获取控件的位置,然后设置点击控件的位置,用于input type=file 上
/// </summary>
public void IEGetCtrlPlace()
{
string sHTML = "";
sHTML += "<script language=\"javascript\">\n";
sHTML += "function fileAttachmentclick(obj)\n";
sHTML += "{\n";
sHTML += " with(obj){\n";
sHTML += "style.posTop=event.srcElement.getBoundingClientRect().top\n";
sHTML += " var x=event.x-offsetWidth/2\n";
sHTML += " if(x<event.srcElement.getBoundingClientRect().left)x=event.srcElement.getBoundingClientRect().left\n";
sHTML += "if(x>event.srcElement.getBoundingClientRect().left+event.srcElement.offsetWidth-offsetWidth)x=event.srcElement.getBoundingClientRect().left+event.srcElement.offsetWidth-offsetWidth\n";
sHTML += "style.posLeft=x\n";
sHTML += "}\n";
sHTML += "}\n";
sHTML += "</script>\n";
Page.RegisterStartupScript("IEGetCtrlPlace", sHTML);
}
执行页面的.cs文件page_load中,加载上面的代码
this.IEGetCtrlPlace();
然后加HTML客户段控件,注意:input type=file需要添加runat="server",这样编程就可以访问到,当然,需要在.cs文件中声明:protected System.Web.UI.HtmlControls.HtmlInputFile fileAttachment;
<input class="txt-main" id="f_file" type="text" size="36" name="f_file">
<input class="btn-main" onmouseover="fileAttachmentclick(fileAttachment)" type="button" value="上传文件">
<INPUT id="fileAttachment" style="FILTER: alpha(opacity=0); WIDTH: 30px; POSITION: absolute" type="file" onchange="f_file.value=this.value" runat="server">
最后添加btn,其事件代码如下:<input class="btn-main" onmouseover="fileAttachmentclick(fileAttachment)" type="button" value="上传文件">
<INPUT id="fileAttachment" style="FILTER: alpha(opacity=0); WIDTH: 30px; POSITION: absolute" type="file" onchange="f_file.value=this.value" runat="server">
if (this.fileAttachment.PostedFile.ContentLength != 0)
{
string sFileListName = Guid.NewGuid().ToString();
string sOriginalFileName = this.fileAttachment.PostedFile.FileName;
sOriginalFileName = sOriginalFileName.Substring(sOriginalFileName.LastIndexOf("\\") + 1);
Directory.CreateDirectory(Server.MapPath(GlobalVar.UploadPath + sFileListName));
this.fileAttachment.PostedFile.SaveAs(Server.MapPath(GlobalVar.UploadPath + sFileListName + "/" + sOriginalFileName));
}
else
{
this.Alert(GlobalVar.SA_RECORD_MISS_REQUIRED_INFO);//提示:缺少必要的信息,请检查。
}
GlobalVar.UpLoadPath是上传文件的路径.
{
string sFileListName = Guid.NewGuid().ToString();
string sOriginalFileName = this.fileAttachment.PostedFile.FileName;
sOriginalFileName = sOriginalFileName.Substring(sOriginalFileName.LastIndexOf("\\") + 1);
Directory.CreateDirectory(Server.MapPath(GlobalVar.UploadPath + sFileListName));
this.fileAttachment.PostedFile.SaveAs(Server.MapPath(GlobalVar.UploadPath + sFileListName + "/" + sOriginalFileName));
}
else
{
this.Alert(GlobalVar.SA_RECORD_MISS_REQUIRED_INFO);//提示:缺少必要的信息,请检查。
}