简体   繁体   English

更新面板内的异步文件上传和触发回发的下拉菜单不起作用

[英]Asynchronous file upload inside a update panel and a dropdown to trigger postback not working

 <asp:AsyncFileUpload ID="AFU_Video" OnUploadedComplete="btnVidUpload_Click" 
             runat="server" UploaderStyle="Traditional" ThrobberID="aajaxLoader" />

I have a Ajax Async File Upload inside a update panel which upload the file asynchronously when a file is selected我在update panel有一个 Ajax 异步文件上传,它在选择文件时异步上传文件

this is the method to upload the file这是上传文件的方法

protected void btnVidUpload_Click(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {
        lbl_VideoLinkName.Visible = true;
        if (AFU_Video.HasFile)
        {            
            //create the path to save the file to  
            string filename = AFU_Video.FileName;
            string Fullpath = Path.Combine(@"D:\Media", filename);
            AFU_Video.SaveAs(Fullpath);
            lbl_VideoLinkName.Text = "You uploaded " + AFU_Video.FileName;
            Hidd_VideoLoc.Value = filename;
        }

In the method I store the location of the file in a Hidd_VideoLoc (hidden field) , below the file upload I have a drop down which does a post back when value is changed.在我将文件的位置存储在 Hidd_VideoLoc (hidden field) ,在文件上传下方,我有一个下拉列表,当值更改时会进行回发。 When the drop down value is changed then the file upload loses the file, but the file is uploaded in the server.当下拉值更改时,文件上传会丢失文件,但文件会上传到服务器中。 I want the file name to update the database when submit button is clicked but the Hidden value also loses the file name and it is empty.我希望文件名在单击提交按钮时更新数据库,但隐藏值也会丢失文件名并且为空。 But the file is present in the server.但是该文件存在于服务器中。 I tried to save the file name value in hidden field in btnVidUpload_Click but it save the value but on SelectedIndexChanged the hidden field loses it value.我试图将文件名值保存在btnVidUpload_Click隐藏字段中,但它保存了该值,但在 SelectedIndexChanged 上,隐藏字段失去了它的值。 How can I store the value in hidden field even after A post back (I dont want to use session)即使在回发后如何将值存储在隐藏字段中(我不想使用会话)

      <asp:DropDownList ID="ddl_Res" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged">
 </ContentTemplate>
  <Triggers>
    <asp:AsyncPostBackTrigger ControlID="ddl_Res" EventName="SelectedIndexChanged" />         
  </Triggers>

The AsyncFileUpload is an asynchronous control in its own right. AsyncFileUpload 本身就是一个异步控件。 Meaning when you upload the file using that control, it is uploaded to the server.这意味着当您使用该控件上传文件时,它会上传到服务器。

Then if you click save to upload again, it will upload that file again, unless when the fileupload control gets to the server it then clears itself.然后,如果您再次单击保存上传,它将再次上传该文件,除非文件上传控件到达服务器时它会自行清除。

Unforunately you have to store that file when it hits the server the first time.不幸的是,当它第一次访问服务器时,您必须存储该文件。 You can use session or store it to the file system, with a known Id, eg 1234.tmp, then grab it when the UpdatePanel is set.您可以使用会话或将其存储到文件系统中,使用已知 ID,例如 1234.tmp,然后在设置 UpdatePanel 时获取它。

Also to make sure you clear the AsyncFileUpload here is what you do.还要确保您在这里清除 AsyncFileUpload 是您要做的。 On the fileupload complete在文件上传完成

 protected void AsyncFileUpload_OnUploadedComplete(object sender, 
                                     AjaxControlToolkit.AsyncFileUploadEventArgs e)
    {

        ((AsyncFileUpload)sender) // You have to save this somewhere, or the information in it


        ((AsyncFileUpload)sender).ClearAllFilesFromPersistedStore();
        ((AsyncFileUpload)sender).ClearFileFromPersistedStore();
        sender = null;
    }

Then on the page use some javascript as such on the upload complete event of the control然后在页面上在控件的上传完成事件上使用一些javascript

function UploadComplete() {
        var btn = document.getElementById('MainContent_btnEnter');

        btn.value = "Enter";
        btn.disabled = false;
        var form1 = document.getElementById('form1');
        form1.target = "";

        document.getElementById('MainContent_upVideo').innerHTML = 'Uploaded Successfully';
        document.getElementById('MainContent_upVideo').value = '';
        document.getElementById('MainContent_upVideo_ClientState').value = '';
    }

Take note that you need to reset the form target.请注意,您需要重置表单目标。 Also take note you need to clear the ClientState value and you can add in some completed text on the page.还要注意您需要清除 ClientState 值,您可以在页面上添加一些完整的文本。 MainContent is just a ContentHolder, so make sure you get the right control name for your page. MainContent 只是一个 ContentHolder,因此请确保为您的页面获取正确的控件名称。

I had same problem couple weeks ago, aspx was very complex and I had to put filepath into session .几周前我遇到了同样的问题, aspx 非常复杂,我不得不将文件filepath放入session I would recommend you to try do this that way.我建议你尝试这样做。 I remember also I couldn't find any other solution on google or here on stack.我还记得我在谷歌或堆栈上找不到任何其他解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM