c# - 如何在 ASP.NET 中上传图像文件而不进行任何回发

标签 c# asp.net

我正在使用 <asp:FileUpload> 上传文件和 <asp:button>控件,但我想在没有回发的情况下进行。单击按钮时,我执行以下代码。

protected void btnUpload_Click(object sender, EventArgs e)
{
    string strFileName = Path.GetFileName(FileUpload1.FileName);  //fileupload1 is the <asp:fileupload ID
    FileUpload1.SaveAs(Server.MapPath("~/UploadFile/" + strFileName + ""));
    imgUpload.ImageUrl = "../UploadFile/" + strFileName + "";  //imgupload is the <img ID on which I am showing the image after upload
    imgUpload.Visible = true;
}

上传文件后,我在我的项目解决方案中显示指定文件夹中保存的图像,但是在单击上传按钮时整个页面都会加载,我不希望在单击上传按钮时回发。

最佳答案

just add the script

    <script language="javascript">
        function Change(obj) {
             __doPostBack("<%= btnUpload.ClientID %>", "");
        }

      </script>


Call the script in your button click event like this 
<pre>
onchange="Change(this);"
</pre>   

Your image control added in the update panel with contentTemplate 

    <asp:FileUpload ID="imgFileUploader" runat="server"   />
             <asp:Button ID="btnUpload" runat="server"  
         Text="Upload" onclick="btnUpload_Click" onchange="Change(this);" />
      <br />
        <br />
     <asp:ScriptManager ID="ScriptManager1" runat="server">
     </asp:ScriptManager>
  &nbsp;<asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>    
               <img id="imgOrginal" runat="server" style="height: 200px; width: 200px;" />
              </ContentTemplate>
      </asp:UpdatePanel>

for more details

关于c# - 如何在 ASP.NET 中上传图像文件而不进行任何回发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14049905/

相关文章:

c# - 是否有类似 ISerializable 的东西适用于 XMLSerializer?

c# - 密集分布式 C# (WCF) 架构设计

c# - 如何在 ASP.NET Core 中创建角色并将其分配给用户?

c# - 如何测量应用程序加载所有使用的程序集所花费的时间?

c# Excel 跳过第一行?

c# - 序列化对象时省略 XML 处理指令

c# - 字符串插值问题

asp.net - 如何在 ASP.Net 站点中启用 FCKeditor 的实时预览?

asp.net - Signalr 在 IIS 7.5 和 Edge/IE 中错误地反序列化我的对象,foreverFrame 损坏了?

c# - 从 JavaScript 中单击按钮/页面提交