简体   繁体   中英

Wait till the file Save complete in Asp.net C#

I have a file upload process in my asp.net C# application. I am using the below code to save the browse file on path:

    this.fupUploadImage.SaveAs("filepath");

I have to wait the execution till the Save process completed. Can any body suggest the good approach ?

Thanks in advance.

SaveAs is a void method so you can wrap that call and return a value when it finishes

int SaveFile(string filePath)
      {            

        if (!System.IO.File.Exists(filePath)) 
        {
            try{
                FileUpload1.SaveAs(savePath);
                return 1;
            }
            catch{

            } 

          return 0;
        }


      }

then you can cal it in this way

if (SaveFile(@"myPath/test.txt") > 0){
     //save ok!
}

this is a Synch approach to your problem. The program waits for the end of the Save process. If you want to execute it in background in another task you can call

   Task<int> t = Task.Run(() => SaveFile(@"myPath/test.txt"))
                              .ContinueWith((i) => i > 0? "saveOk" : "saveFailed");

now the save operation is executed in background and at the end the result arrives in input in ContinueWith method and you can replace my labels "saveOk" and "saveFailed" with code to manage when save operation is succesfull or not.

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