单击按钮即可重置文件上载 [英] File upload is resetting on click of button

查看:71
本文介绍了单击按钮即可重置文件上载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在asp.net应用程序中使用fileupload控件。但是这个控件添加了一个默认的上传按钮浏览和一个文本框,其中选择文件后显示路径。点击浏览按钮我上传文件并点击提交按钮。第一次,点击提交按钮,上传的文件是自动删除并无法提交表单。点击提交按钮我可以提交按钮。



我不明白为什么文件会自动删除它在本地系统中运行正常。当我在测试服务器中复制文件时。这个问题上升了。



我尝试了什么:



函数DisplayFileDescription(){



var tb1 = document.getElementById(<%= txtCourseCode .ClientID%>)。value;

var tb2 = document.getElementById(<%= txtExamNumber.ClientID%>)。value;



if(tb1 ==''){

document.getElementById(<%= rfvCourseCode.ClientID% >)。style.visibility =visible;

document.getElementById(<%= rfvCourseCode.ClientID%>)。enabled = true;

//清除();

}

if(tb2 ==''){

document.getElementById(<%= rfvExamNumber.ClientID%>)。style.visibility =visible;

document.getElementById(<%= rfvExamNumber.ClientID%>)。enabled = true;

//清除();

}

if(tb1!=''&& tb2!=''){

document.getElementById(<%= rfvCourseCode.ClientID%>)。style.visibility =hidden;

文件.getElementById(<%= rfvCourseCode.ClientID%>)。enabled = false;

document.getElementById(<%= rfvExamNumber.ClientID%>)。style.visibility =隐藏;

document.getElementById(<%= rfvExamNumber.ClientID%>)。enabled = false;

}



// var strcollege = document.getElementById(ctl00 $ ContentPlaceHolder1 $ ddlCollege)。options [e.selectedIndex] .text;

var College = document。 getElementById(ddlCollege);

var strcollege = College.options [College.selectedIndex] .text;



// UI Studnet

if(strcollege ==爱荷华大学){

var strFileDescription = document.getElementById('< %= txtCourseCode.ClientID%>')。value;

// var strFileDescription = document.getElementById('<%= txtCourseCode.ClientID%>')。value +_+ document.getElementById('<%= txtExamNumber.ClientID%>')。value;



// strFileDescription = removeSpecial(strFileDescription);

strFileDescription = strFileDescription.split(':')。join();

document.getElementById('<%= txtFileDescription.ClientID%>')。value = strFileDescription;



if(document.getElementById(FileUploadAttachment)。value!=''){

var filename = document.getElementById( FileUploadAttachment)。value;



document.getElementById(lblFileChoosen)。innerHTML = filename.substring(filename.lastIndexOf('\\')+ 1 );



}

}

//非UI学生

else {

var strFileDescription = document.getElementById('<%= txtStudentFirstName.ClientID%>')。value + document.getElementById('<%= txtStudentLastName.ClientID%>')。value +_+

document.getElementById('< %= txtCourseCode.ClientID%>')。value +_+ document.getElementById('<%= txtExamNumber.ClientID%>')。value;



strFileDescription = strFileDescription.split(':')。join();

document.getElementById('<%= txtFileDescription.ClientID%>')。value = strFileDescription;



if(document.getElementById(ContentPlaceHolder1_FileUploadAttachment)。value!=''){

var filename = document.getElementById( ContentPlaceHolder1_FileUploadAttachment)。value;



document.getElementById(lblFileChoosen)。innerHTML = filename.substring(filename.lastIndexOf('\\')+ 1);

}

}



}

I am using fileupload control in my asp.net application. But this control add a default upload button browse and a textbox where path is shown after selecting file.Onclick of browse button I am uploading the file and clicking on submit button.For the first time,On click of submit button, the uploaded file is automatically deleting and unable to submit the form.On click of Submit button I am able to submit the button.

I did n't understand why the file is automatically deleting.It works fine in the local system.When i copy then files in test server.This issue rises.

What I have tried:

function DisplayFileDescription() {

var tb1 = document.getElementById("<%=txtCourseCode.ClientID %>").value;
var tb2 = document.getElementById("<%=txtExamNumber.ClientID %>").value;

if (tb1 == '') {
document.getElementById("<%=rfvCourseCode.ClientID%>").style.visibility = "visible";
document.getElementById("<%=rfvCourseCode.ClientID%>").enabled = true;
// Clear();
}
if (tb2 == '') {
document.getElementById("<%=rfvExamNumber.ClientID%>").style.visibility = "visible";
document.getElementById("<%=rfvExamNumber.ClientID%>").enabled = true;
// Clear();
}
if(tb1!='' && tb2!='') {
document.getElementById("<%=rfvCourseCode.ClientID%>").style.visibility = "hidden";
document.getElementById("<%=rfvCourseCode.ClientID%>").enabled = false;
document.getElementById("<%=rfvExamNumber.ClientID%>").style.visibility = "hidden";
document.getElementById("<%=rfvExamNumber.ClientID%>").enabled = false;
}

//var strcollege = document.getElementById("ctl00$ContentPlaceHolder1$ddlCollege").options[e.selectedIndex].text;
var College = document.getElementById("ddlCollege");
var strcollege = College.options[College.selectedIndex].text;

//UI Studnet
if (strcollege == "University of Iowa") {
var strFileDescription = document.getElementById('<%=txtCourseCode.ClientID %>').value;
// var strFileDescription = document.getElementById('<%=txtCourseCode.ClientID %>').value + "_" + document.getElementById('<%=txtExamNumber.ClientID %>').value;

//strFileDescription = removeSpecial(strFileDescription);
strFileDescription = strFileDescription.split(':').join("");
document.getElementById('<%=txtFileDescription.ClientID%>').value = strFileDescription;

if (document.getElementById("FileUploadAttachment").value != '') {
var filename = document.getElementById("FileUploadAttachment").value;

document.getElementById("lblFileChoosen").innerHTML = filename.substring(filename.lastIndexOf('\\') + 1);

}
}
//Non UI Student
else {
var strFileDescription = document.getElementById('<%=txtStudentFirstName.ClientID %>').value + document.getElementById('<%=txtStudentLastName.ClientID %>').value + "_" +
document.getElementById('<%=txtCourseCode.ClientID %>').value + "_" + document.getElementById('<%=txtExamNumber.ClientID %>').value;

strFileDescription = strFileDescription.split(':').join("");
document.getElementById('<%=txtFileDescription.ClientID%>').value = strFileDescription;

if (document.getElementById("ContentPlaceHolder1_FileUploadAttachment").value != '') {
var filename = document.getElementById("ContentPlaceHolder1_FileUploadAttachment").value;

document.getElementById("lblFileChoosen").innerHTML = filename.substring(filename.lastIndexOf('\\') + 1);
}
}

}

<table runat="server" id="tblExamFileAttachments" frame="box" visible="false" width="800px"

                              cellpadding="0" cellspacing="0">
                              <tr>
                                  <td width="10px">
                                  </td>
                                  <td style="font-weight: bold" colspan="2">
                                      Upload Exam Attachment File
                                      <br />
                                      <br />
                                  </td>
                              </tr>
                              <tr>
                                  <td width="10px">
                                  </td>
                                  <td style="font-weight: bold">
                                      Please select File :
                                  </td>
                                  <td>
                                      <asp:FileUpload ID="FileUploadAttachment" runat="server" size="50" onchange="DisplayFileDescription()" />&nbsp;
                                      <asp:Label ID="lblFileChoosen" runat="server" Text=""></asp:Label>
                                  </td>
                              </tr>
                              <tr>
                                  <td width="10px">
                                  </td>
                                  <td style="font-weight: bold">
                                      File Description:
                                  </td>
                                  <td>
                                      <asp:TextBox runat="server" Width="300px" ID="txtFileDescription">
                                      </asp:TextBox>
                                  </td>
                              </tr>
                          </table>





< asp:Button ID =btnSubmitrunat =serverText =SubmitOnClick =btnSubmit_Click

Font-Bold =trueCssClass =SubmitButtonValidationGroup = SubmitGroup/>



<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"
Font-Bold="true" CssClass="SubmitButton" ValidationGroup="SubmitGroup" />

推荐答案

ContentPlaceHolder1
ContentPlaceHolder1


ddlCollege)。options [e.selectedIndex] .text;

var College = document.getElementById(ddlCollege);

var str college = College.options [College.selectedIndex] .text;



// UI Studnet

if(strcollege ==爱荷华大学) ){

var strFileDescription = document.getElementById('<%= txtCourseCode.ClientID%>')。value;

// var strFileDescription = document.getElementById ('<%= txtCourseCode.ClientID%>')。value +_+ document.getElementById('<%= txtExamNumber.ClientID%>')。value;



// strFileDescription = removeSpecial(strFileDescription);

strFileDescription = strFileDescription.split(':')。join();

文件.getElementById('<%= txtFileDescription.ClientID%>')。value = strFileDescription;



if(document.getElementById(FileUploadAttachment)。value! =''){b / b
var filename = document.getElementB yId(FileUploadAttachment)。value;



document.getElementById(lblFileChoosen)。innerHTML = filename.substring(filename.lastIndexOf('\\' )+ 1);



}

}

//非UI学生

else {

var strFileDescription = document.getElementById('<%= txtStudentFirstName.ClientID%>')。value + document.getElementById('<%= txtStudentLastName.ClientID%> ')。value +_+

document.getElementById('<%= txtCourseCode.ClientID%>')。value +_+ document.getElementById('<%= txtExamNumber.ClientID%>')。value;



strFileDescription = strFileDescription.split(':')。join();

document.getElementById('<%= txtFileDescription.ClientID%>')。value = strFileDescription;



if(document.getElementById(ContentPlaceHolder1_FileUploadAttachment)。value!=''){

var filename = document.getElementById(ContentPlaceHolder1_FileUploadAttachment)。value;



document.getElementById(lblFileChoosen)。innerHTML = filename.substring(filename.lastIndexOf('\\')+ 1);

}

}



}

ddlCollege").options[e.selectedIndex].text;
var College = document.getElementById("ddlCollege");
var strcollege = College.options[College.selectedIndex].text;

//UI Studnet
if (strcollege == "University of Iowa") {
var strFileDescription = document.getElementById('<%=txtCourseCode.ClientID %>').value;
// var strFileDescription = document.getElementById('<%=txtCourseCode.ClientID %>').value + "_" + document.getElementById('<%=txtExamNumber.ClientID %>').value;

//strFileDescription = removeSpecial(strFileDescription);
strFileDescription = strFileDescription.split(':').join("");
document.getElementById('<%=txtFileDescription.ClientID%>').value = strFileDescription;

if (document.getElementById("FileUploadAttachment").value != '') {
var filename = document.getElementById("FileUploadAttachment").value;

document.getElementById("lblFileChoosen").innerHTML = filename.substring(filename.lastIndexOf('\\') + 1);

}
}
//Non UI Student
else {
var strFileDescription = document.getElementById('<%=txtStudentFirstName.ClientID %>').value + document.getElementById('<%=txtStudentLastName.ClientID %>').value + "_" +
document.getElementById('<%=txtCourseCode.ClientID %>').value + "_" + document.getElementById('<%=txtExamNumber.ClientID %>').value;

strFileDescription = strFileDescription.split(':').join("");
document.getElementById('<%=txtFileDescription.ClientID%>').value = strFileDescription;

if (document.getElementById("ContentPlaceHolder1_FileUploadAttachment").value != '') {
var filename = document.getElementById("ContentPlaceHolder1_FileUploadAttachment").value;

document.getElementById("lblFileChoosen").innerHTML = filename.substring(filename.lastIndexOf('\\') + 1);
}
}

}
<table runat="server" id="tblExamFileAttachments" frame="box" visible="false" width="800px"

                              cellpadding="0" cellspacing="0">
                              <tr>
                                  <td width="10px">
                                  </td>
                                  <td style="font-weight: bold" colspan="2">
                                      Upload Exam Attachment File
                                      <br />
                                      <br />
                                  </td>
                              </tr>
                              <tr>
                                  <td width="10px">
                                  </td>
                                  <td style="font-weight: bold">
                                      Please select File :
                                  </td>
                                  <td>
                                      <asp:FileUpload ID="FileUploadAttachment" runat="server" size="50" onchange="DisplayFileDescription()" />&nbsp;
                                      <asp:Label ID="lblFileChoosen" runat="server" Text=""></asp:Label>
                                  </td>
                              </tr>
                              <tr>
                                  <td width="10px">
                                  </td>
                                  <td style="font-weight: bold">
                                      File Description:
                                  </td>
                                  <td>
                                      <asp:TextBox runat="server" Width="300px" ID="txtFileDescription">
                                      </asp:TextBox>
                                  </td>
                              </tr>
                          </table>





< asp:Button ID =btnSubmitrunat =serverText =SubmitOnClick =btnSubmit_Click

Font-Bold =trueCssClass =SubmitButtonValidationGroup =SubmitGr oup/>



<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"
Font-Bold="true" CssClass="SubmitButton" ValidationGroup="SubmitGroup" />


你看到的是正常的,这是因为出于安全原因。在Web中搜索它为什么会执行该行为将为您提供大量结果。以下是一个简短的讨论: asp.net - FileUpload控件自行清除 - Stack Overflow [ ^ ]



如果您不想被回发打扰,您可能需要切换到基于客户端的文件上传。
What you were seeing is normal, and it's because for security reasons. Searching for it in the web why it does that behavior will give you loads of results. Here's one short discussion: asp.net - FileUpload control clears itself - Stack Overflow[^]

You may need to switch to a client-side based file uploading if you don't want to be bothered by postbacks.


这篇关于单击按钮即可重置文件上载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆