【问题标题】:Any idea how can I keep file between postback calls?知道如何在回发调用之间保存文件吗?
【发布时间】:2016-03-03 11:57:39
【问题描述】:

这是 ASP.NET 代码:

    <div class="row1" style="padding: 3px">
      <asp:Button Text="Select" ID="btnDescColumn" runat="server" OnClick="SetDescPoint" CausesValidation="False"/>

      <input type="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" id="fileSelect" runat="server" class="hidden" />
      <asp:Button ID="btnUpload" runat="server" Text="Load" OnClick="LoadFile" class="hidden" CausesValidation="False" />
      <input type="button" id="triggerUpload" name="name" value="Select File" />
    </div>  

这里是视图:

这里是 JQuery 代码:

    $('#triggerUpload').click(function () {
        $('#<%=fileSelect.ClientID%>').trigger('click');
    });

    $('#<%=fileSelect.ClientID%>').change(function () {
        $('#<%=btnUpload.ClientID%>').trigger('click');
    });

当点击Select File 按钮时,对话窗口打开并且用户选择文件。 选择文件后,此 JQuery 代码触发:

    $('#<%=fileSelect.ClientID%>').change(function () {
        $('#<%=btnUpload.ClientID%>').trigger('click');
    });

上面的 JQuery 行触发了这个 asp 按钮控件:

<asp:Button ID="btnUpload" runat="server" Text="Load" OnClick="LoadFile" class="hidden" CausesValidation="False" />

这是触发此代码背后的方法:

    protected void LoadFile(object sender, EventArgs e) 
    {
        HttpPostedFile file = Request.Files[fileSelect.Name];
        int fileSize = file.ContentLength;
        byte[] fileByteArray = new byte[fileSize];
        file.InputStream.Read(fileByteArray, 0, fileSize);
    }

在点击 postBack Process 按钮并触发后面的代码后:

    protected void SetDescPoint(object sender, EventArgs e)
    {
        if(fileSelect.Value != string.Empty)
        {
           //make some process...
        } 
   }

但是fileSelect 控件是空的。

据我了解 input file 在回发调用之间不保留文件。

我需要在后面的 SetDescPoint 代码中访问该文件。

知道如何在回发调用之间保存文件吗?

【问题讨论】:

    标签: c# asp.net file-io


    【解决方案1】:

    当你uploading a file in asp.net .. 你必须做一个full post-back .. 这只能通过submitting the page (button click).. no jquery trick will work .. 即使你的postback button is under a updatepanel .. 你也可以will not get the physical file in server side

    【讨论】:

    • 我实现了完整的回发。但是我需要在第二次回发后访问文件
    • 浏览器不会在下次回发中发送文件
    • 这是不可能的,因为在完整的回发server 发送响应到browser 并且dom 通过来自服务器的响应重新加载.. 并且obisouly 服务器没有将文件发送回浏览器.. 所以这是协议...
    • 回发后访问文件还有其他解决方案吗?
    • 请不要使用inline code 强调,这会使您的帖子难以阅读。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-31
    • 1970-01-01
    • 2020-11-02
    • 2012-09-26
    • 1970-01-01
    • 2016-02-25
    • 2011-10-11
    相关资源
    最近更新 更多