_doPostBack 无效有关问题
__doPostBack 无效问题
这个是选择文件直接上传,直接触发按钮btnupload 上传文件 ,在本地没有问题,但发布到服务器后,点上传后弹出 文件选择框,选择文件后,没反应压根没到按钮的方法,应该是JS __doPostBack('btnupload', ''); 没触发这个按钮。
请问大侠指点 谢谢!
前台:
HTML:
JS:
后台注册事件:
------解决思路----------------------
为啥要这样呢?执行form.submit()不行吗?
------解决思路----------------------
用 asp:button
------解决思路----------------------
要生成 javascript 代码,应该写
而不是去写什么 __doPostBack(.....)。不是不能写,而是你以为你知道写法但实际上写不好。所以应该用 asp.net 给你生成 javascript代码,而不是手写。
这个是选择文件直接上传,直接触发按钮btnupload 上传文件 ,在本地没有问题,但发布到服务器后,点上传后弹出 文件选择框,选择文件后,没反应压根没到按钮的方法,应该是JS __doPostBack('btnupload', ''); 没触发这个按钮。
请问大侠指点 谢谢!
前台:
HTML:
<asp:FileUpload ID="FileUpload1" runat="server" onchange="uploadFile(this.value)" style="visibility:hidden;" />
<button id="btnupload" type="submit" class="btn blue pull-right" runat="server" style="visibility:hidden;" ></button>
JS:
//弹出选择文件框
function uploadFile(filePath) {
//alert(filePath);
if (filePath.length > 0) {
__doPostBack('btnupload', '');
}
}
function openBrowse() {
var ie = navigator.appName == "Microsoft Internet Explorer" ? true : false;
if (ie) {
document.getElementById("FileUpload1").click();
}
else {
var a = document.createEvent("MouseEvents");//FF的处理
a.initEvent("click", true, true);
document.getElementById("FileUpload1").dispatchEvent(a);
}
}
后台注册事件:
protected void Page_Load(object sender, EventArgs e)
{
btnupload.ServerClick += new EventHandler(btnupload_ServerClick);
}
//单传
void btnupload_ServerClick(object sender, EventArgs e)
{
try
{
if (FileUpload1.PostedFile.FileName == "")
{
Cbll.Mytip(Page, "No File!");
return;
}
else
{
//2:保存附件
string filepath = FileUpload1.PostedFile.FileName; //文件路径
string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1); //原名
//取得文件名(包括路径)里最后一个"."的索引
int index = filename.LastIndexOf(".");
//取得文件扩展名
string extendName = filename.Substring(index).ToLower();
//string s_rpath = FileHelper.GetUploadPath();
string s_rpath = GetUploadPath();
string Datedir = DateTime.Now.ToString("yy-MM");
string updir = s_rpath + "\\" + Datedir;
if (!Directory.Exists(updir))
{
Directory.CreateDirectory(updir);
}
//2:保存数记录
//增加对应的附件记录
if (this.lblProjId.Text != null && this.txtStepId.Text != null && Session["Userid"] != null)
{
pf.ProjId = Convert.ToInt32(this.lblProjId.Text); //项目ID
pf.ProjectName = this.txtProjName.Text;//项目名称
pf.StepId = Convert.ToInt32(this.txtStepId.Text);// 步骤ID
pf.CreateUserId = Convert.ToInt32(Session["Userid"].ToString()); //
}
//获取相关信息:项目、作业、步骤名称。
myReader=fbll.UploadBaseInfo(pf);
while(myReader.Read())
{
pf.ProjectName = myReader[0].ToString();
pf.TaskName=myReader[2].ToString();
pf.StepName = myReader[1].ToString();
}
//上传相关信息
string FileNo = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
//string fullname = FileNo + "_" + filename; //编号+时间+原文件名【之前】
string fullname = pf.ProjectName + "_" + pf.TaskName + "_" + pf.StepName + extendName; //项目名+作业名+步骤名称+扩展名 (重复则不给他保存,此系统这个命名规则是保证唯一的)
//过滤
fullname = CleanInvalidFileName(fullname);
string serverpath = updir + fullname;
//FileUpload1.PostedFile.SaveAs(serverpath);
pf.FileNo = FileNo; //编号时间
pf.FileName = filename; //原文件名
pf.FilePath = "//" + "upload" + "//" + Datedir + "//" + fullname; //全路径
if (!File.Exists(serverpath))
{
if (fbll.InsertUploadFile(pf) > 0)
{
FileUpload1.PostedFile.SaveAs(string.Format("{0}\\{1}", updir, fullname));
// 刷新页面
Response.Write("<script>self.location.href=self.location.href;</script>");
}
}
else
{
//上传失败
// 刷新页面
Response.Write("<script>self.location.href=self.location.href;</script>");
}
}
}
catch (Exception ex)
{
//this.lb_info.Text = "上传发生错误!原因是:" + ex.ToString();
Console.WriteLine(System.Text.RegularExpressions.Regex.Match(ex.StackTrace, @"Rows:\D+\d+").Value);
}
}
------解决思路----------------------
为啥要这样呢?执行form.submit()不行吗?
------解决思路----------------------
用 asp:button
------解决思路----------------------
要生成 javascript 代码,应该写
this.ClientScript.GetPostBackEventReference(this.btnupload, string.Empty)e
而不是去写什么 __doPostBack(.....)。不是不能写,而是你以为你知道写法但实际上写不好。所以应该用 asp.net 给你生成 javascript代码,而不是手写。