ASP.NET 中FileUpload与UpdatePanel共用时FileUpload无法取到文件名信息

在网上搜索发现可以使用PostBackTrigger ,通过设置Trigger来实现共处。

        PS:不要直接Copy网页内的代码到自己的程序内,这样会有一定机率出现无法实现其代码功能的现象,请手动输入代码

 

以下内容转自其他人的博客,转载地址:http://blog.csdn.net/linkinwhite/article/details/5692256,在此谢谢此博客的主人。

 

今天往一个页面中添加图片上传功能,弄了半天发现不成功,FileUpload始终找不到文件,HasFile属性始终为false;经过多次检查后,没发现和原来图片上传有什么区别,都是在UpdatePanel下设置提交按钮的Triggers,感觉很奇怪,最终在网上找到了这么一段话:

解决办法就是UpdatePanel中设置PostBackTrigger:
<asp:UpdatePanel ID="UpdatePanel1" runat="server"   UpdateMode="Conditional">
<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);
}

原来我在开始设置了两个Panel的Visible为false,通过RadioButtonList来切换,导致FileUpload找不到文件的原因,我还在奇怪为什么在生成的HTML中查看不了代码呢,呵呵。UpdatePanel只显示初始页面的HTML代码

 

---------------声明:经过一些测试之后,发现上面的代码并不能很好的解决问题,所以现在使用另一种方式 ,但会回一次全页面刷新的事件。

1.首先在页面内创建DIV与Iframe,使它们独立与现在有的DIV或TABLE之外,如下面代码 :

    </table>   
    </div>
     <div id="divSelectFile" style=" background-color: #FFFFCC; z-index:10000; position:absolute; width: 450px; height: 100px; left: 25%; top: 30%; border:1px solid #CCC;display:none; " >        
        &nbsp;&nbsp;<asp:Label ID="Label10" runat="server" Text="请选择要读取的EXCEL文件....." ></asp:Label><br/><br/>
        &nbsp;&nbsp;<asp:FileUpload ID="FileUploadSelect" runat="server"  Width="350px"  CssClass="button_height" />&nbsp;
        <asp:Button ID="btnConfirmSelectRead" runat="server" Text="确定"  CssClass ="button_height" οnclick="btnConfirmSelectRead_Click" />   
    </div>
    <iframe id="iframeBlock"  src=""  scrolling="no" frameborder="0" style="position:absolute; display:none; width: 450px; height:100px;">
    </iframe>
    </form>
</body>
</html>

在Updatepanel内创建一个调用一段JavaScript代码来控制DIV与Iframe的显示与隐藏:

                                        <asp:TextBox ID="txtFilePath" runat="server" Width="300" ReadOnly="True" ></asp:TextBox>&nbsp;
                                        <input id="btnBrowseExcel" type="button" value="浏览" οnclick="DisplaySelectExcel()" /> &nbsp; 

 

下面是Javascript代码:

    function DisplaySelectExcel() {
        var iFrameShow = document.getElementById("iframeBlock");
        var SelectFile = document.getElementById("divSelectFile");
       if (iFrameShow.style.display == "block") {
            iFrameShow.style.display = "none";
            SelectFile.style.display = "none";
        }
        else {        
            iFrameShow.style.display = "block";
            SelectFile.style.display = "block";
            iFrameShow.style.left = SelectFile.style.left;
            iFrameShow.style.top = SelectFile.style.top;
            iFrameShow.style.width = SelectFile.offsetWidth;
            iFrameShow.style.height = SelectFile.offsetHeight;
            iFrameShow.style.zIndex = SelectFile.style.zIndex - 1;             
        }
    }

 

当上面的全部创建完成后,就可以编写刚刚创建DIV的确定按钮CS代码了。

 protected void btnConfirmSelectRead_Click(object sender, EventArgs e)
    {
        //上传文件至临时目录
        try
        {
            if (FileUploadSelect.HasFile)
            {
                string sFileExt = System.IO.Path.GetExtension(FileUploadSelect.FileName).ToUpper();
                if (sFileExt == ".XLS" || sFileExt == ".XLSX")
                {
                    FileUploadSelect.SaveAs(Server.MapPath("temp") + "\\" + FileUploadSelect.FileName);
                    //sFilePath = Server.MapPath("temp") + "\\" + FileUploadSelect.FileName;
                }
                else
                {
                    ShowOpenFileNotes.Text = "上传的文件格式不正确,请重新选择文件!";
                    return;
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "Return FileName", "<script language='javascript'>alert('请选择目标文件!');</script>");
                return;
            }
        }
        catch
        {
            ShowOpenFileNotes.Text = "上传文件出错,请确认系统上传功能!";
            return;
        }
       


...............读取文件内容............................................................................

 

        try
        {
            System.IO.FileInfo sTempFile = new System.IO.FileInfo(sFilePath);
            sTempFile.Delete();
        }
        catch
        {
            ShowOpenFileNotes.Text = "文件资源回收失败!";
            return;

        }

 

 

 

         

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ASP.NET ,可以使用以下步骤将 FileUpload 控件的图像和其他信息保存到 SQL Server 数据库的 image 字段: 1. 设置数据库连接字符串。 2. 在 ASP.NET 页面添加 FileUpload 控件和其他输入控件(如文本框)来获其他信息。 3. 在服务器端代码,使用 FileUpload 控件的 SaveAs 方法将图像保存到服务器上的某个文件夹。 4. 使用 System.IO.File 类的 ReadAllBytes 方法将保存的图像读入字节数组。 5. 创建 SQL Server 数据库连接并打开连接。 6. 创建 SQL INSERT 语句,将图像字节数组和其他信息插入到数据库的 image 字段。 7. 执行 SQL INSERT 语句并关闭数据库连接。 以下是一个示例代码: ```csharp protected void btnSubmit_Click(object sender, EventArgs e) { // 获文件名和扩展名 string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName); string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName); // 保存文件到服务器上的某个文件夹 string filePath = Server.MapPath("~/Images/") + fileName; FileUpload1.SaveAs(filePath); // 读入图像字节数组 byte[] imageData = File.ReadAllBytes(filePath); // 获其他信息 string name = txtName.Text; string description = txtDescription.Text; // 创建数据库连接 using (SqlConnection connection = new SqlConnection("your_connection_string_here")) { // 打开连接 connection.Open(); // 创建 SQL INSERT 语句 string sql = "INSERT INTO MyTable (Name, Description, ImageData) VALUES (@Name, @Description, @ImageData)"; // 创建 SqlCommand 对象 using (SqlCommand command = new SqlCommand(sql, connection)) { // 添加参数 command.Parameters.AddWithValue("@Name", name); command.Parameters.AddWithValue("@Description", description); command.Parameters.AddWithValue("@ImageData", imageData); // 执行 SQL INSERT 语句 command.ExecuteNonQuery(); } // 关闭连接 connection.Close(); } } ``` 请注意,此示例代码仅供参考。您需要根据自己的实际情况对其进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值