2

I want to wait for SaveAs to complete before continuing, I've tried a few different solutions but it still just continues immediately, not waiting for the upload to complete. What I've tried:

Creating a wrapping function to return an int (this is what I found on Stack Overflow):

    public int SaveFile(HttpPostedFileBase file, string filePath)
    {
        if (!System.IO.File.Exists(filePath))
        {
            try
            {
                file.SaveAs(filePath);

                return 1;
            }
            catch
            {

            }
        }
        return 0;
    }

And running this in my main method:

var success = SaveFile(hpf, savedFileName);

I also tried checking to see if the file is ready:

while (!IsFileReady(filePath))
{
     System.Threading.Thread.Sleep(500);
}

Calling this:

    [Authorize(Roles = "L,I")]
    public static bool IsFileReady(String sFilename)
    {
        // If the file can be opened for exclusive access it means that the file
        // is no longer locked by another process.
        try
        {
            using (FileStream inputStream = System.IO.File.Open(sFilename, FileMode.Open, FileAccess.Read, FileShare.None))
            {
                if (inputStream.Length > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }

            }
        }
        catch (Exception)
        {
            return false;
        }
    }

Again, no luck. I just need to make sure file.SaveAs(...) waits until the file completes uploading to continue.

kgst
  • 243
  • 1
  • 2
  • 13

0 Answers0