UpdatePanel上使用FileUpload上传文件代码

updatepanel上使用fileupload上传文件代码

@page language="c#" masterpagefile="~/evamanager.master" autoeventwireup="true" codefile="hv_excel.asp教程x.cs" inherits="hv_excel" title="untitled page" %>
<asp:content id="content1" contentplaceholderid="contentplaceholder1" runat="server">

<script>
  function callback(filename)
  {
  document.getelementbyid('attach1').innerhtml=filename;
  }
  </script>

  <fieldset>
  <legend>组长及联动对象上传</legend>
  <iframe id="file" name="file" src="attachment.aspx"></iframe>
   
  <asp:textbox id="attach1" runat="server"></asp:textbox>
  </fieldset>
   

</asp:content>
attachment.aspx

<%@ page language="c#" autoeventwireup="true" codefile="attachment.aspx.cs" inherits="attachment" %>

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
  <title>无标题页</title>
  <script>
  window.top.callback(filename);
  </script>

</head>
<body>
  <form id="form1" runat="server">
  <div>
  <asp:fileupload id="fileupload1" runat="server" />
  <asp:button id="button1" runat="server" text="ok" onclick="button1_click" />
  </div>

  </form>
</body>
</html>

attachment.aspx.cs
using system;
using system.data;
using system.configuration;
using system.collections;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;

public partial class attachment : system.web.ui.page
{
  protected void page_load(object sender, eventargs e)
  {

  }

 

  protected void button1_click(object sender, eventargs e)
  {
  if (fileupload1.hasfile == false)
  {
  scriptmanager.registerstartups教程cript(this.page, this.gettype(), "alert", "alert('未指定上传文件');", true);
  return;
  }
  if (fileupload1.filename.substring(fileupload1.filename.lastindexof('.') + 1, 3).tostring().tolower() != "xls")
  {
  scriptmanager.registerstartupscript(this.button1, this.gettype(), "alert", "alert('文件格式错误!请确保为本页面下载excel!');", true);
  return;
  }

  string savepath = request.physicalapplicationpath + "\upfile\" + fileupload1.filename;

  fileupload1.saveas(savepath);

  }

}

//方法二

<asp:updatepanel id="updatepanel1" runat="server">
<contenttemplate>
<asp:fileupload id="fileupload1" runat="server" />
<asp:button id="button1" runat="server" text="上传" onclick="button1_click" />
</contenttemplate>
<triggers>
<asp:postbacktrigger controlid="button1" />
</triggers>
</asp:updatepanel>

而如果你又想在这个updatepanel上做点花样,比如加了一个asp:panel, 可以通过按钮事件触发隐藏或显示的,你会发现fileupload1并不能找到文件。。。

其实道理很简单,updatepanel中的内容是通过xmlhttp实时填充的,在你让他显示之前,查看页面源代码里面是空的。一个动态控件更新普通数据没问题,但上传文件就不行了,我的解决办法是用普通div代替asp:panel,并写了2个函数来动态发送控制脚本,按钮事件中只要调用该函数即可:

<div id="panel1"></div>

private void showpanel()
{
string script = "document.getelementbyid('panel1').style.display='';";
scriptmanager.registerstartupscript(this.page, this.gettype(), "showpanel", script, true);
}
private void closepanel()
{
string script = "document.getelementbyid('panel1').style.display='none';";
scriptmanager.registerstartupscript(this.page, this.gettype(), "closepanel", script, true);
}

 

时间: 2024-08-03 14:58:54

UpdatePanel上使用FileUpload上传文件代码的相关文章

php curl 上传文件代码实例

  这篇文章主要介绍了php curl 上传文件代码实例,本文给出了两种实现方法,并分别给出了实现代码,需要的朋友可以参考下 假设server端上传文件处理脚本upload.php: 代码如下: print_r($_POST); print_r($_FILES); 1.使用 CURL 默认的方法 复制代码 代码如下: //如果php文件是utf8编码,系统是GBK编码,那么就需要转下编码,要不然Php在系统中找不到这个文件 $file = realpath(mb_convert_encoding

php上传文件代码

原文地址:php上传文件代码作者:风云之士  function Upload($uploaddir)      {         $tmp_name =$_FILES['file']['tmp_name'];  // 文件上传后得临时文件名          $name     =$_FILES['file']['name'];     // 被上传文件的名称         $size     =$_FILES['file']['size'];    //  被上传文件的大小        

java中使用Struts 2框架上传文件代码

HTML表单默认值:  代码如下 复制代码 <form action="" enctype="application/x-www-form-urlencoded" method="get"> ...</form> 要使用HTML表单上传文件,需要将HTML表单的enctype属性设置为"multipart/form-data",还需要将表单的method属性设置为"post"  代码

PHP上传文件代码之入门代码

这是一种非常简单文件上传方式.基于安全方面的考虑,您应当增加有关什么用户有权上传文件的限制.  代码如下 复制代码 <!DOCTYPE html>       <html>       <head>       <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />       <meta http-equiv="Co

iis服务器接收post上传文件代码求助

问题描述 <html><head></head><body><scriptLanguage="C#"AutoEventWireup="true"encType="multipart/form-data"ruat="server">protectedvoidPage_Load(objectsender,EventArgse){try{foreach(stringfinR

PHP+Ajax实现文件无刷新上传文件代码

PHP + jQuery Ajax文件上传实例.因为看到一些朋友询问如何实现PHP环境下的网页上传功能,自己这几天刚用了jQuery_upload_multiple上传插件,所以在这里把用法给大家说一下. 要实现基于这个插件的上传功能,其实挺简单,需要jquery就行了,另外还有一个上传文件时的PHP程序,费话不多说,先看下面的HTML,也就是带有上传表单,让用户选择上传文件的页面:    代码如下 复制代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML

php curl 上传文件代码实例_php技巧

假设server端上传文件处理脚本upload.php: 复制代码 代码如下: <?php    print_r($_POST);  print_r($_FILES);  1.使用 CURL 默认的方法 复制代码 代码如下: //如果php文件是utf8编码,系统是GBK编码,那么就需要转下编码,要不然Php在系统中找不到这个文件    $file = realpath(mb_convert_encoding('测试图片.JPG','GBK','utf8'));    $file = realp

求助,asp.net web.上传文件代码异常

问题描述 如图,上传文件到指定目录后没法点击"考贝路径"按钮.代码如下:aspx:<htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>上传文件</title><linkhref="StyleSheet.css"rel="stylesheet"type="tex

求struts2上传文件代码,最好详细些。新手

问题描述 急,求上传例子,百度那些我都看过,有没有真实的例子. 解决方案 解决方案二: 解决方案三:马上能给你写一个简单的例子,^_^解决方案四:你自己参照着改成s2的吧解决方案五:action://文件上传下载相关属性privateFilefile;privateStringfilename;privateStringfileFileName;privateStringfileContentType;privateStringsavePath;//上传文件publicStringupload(