简体   繁体   中英

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

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. 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. 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. 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.

Also to make sure you clear the AsyncFileUpload here is what you do. 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

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. MainContent is just a ContentHolder, so make sure you get the right control name for your page.

I had same problem couple weeks ago, aspx was very complex and I had to put filepath into 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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