Problem sending file to server when in OneDrive

19 Views Asked by At

When the Excel file is synchronized via OneDrive and is open in the Excel application, when you try to select the file, the message "The file is in use" appears and this is correct. However, when I drag and drop the file, no message appears, but when sending the file to the server, the file is not sent.

Is there any way to check if a file is being used by another process? To be able to display the message yourself.

1

There are 1 best solutions below

0
jakub podhaisky On

if you are working in a web enviroment you will need to use a server side validation because the JS cannot access files due to the security measures... This is how I would do this in asp.net:

public JsonResult CheckFileStatus(string filePath)
{
    try
    {
        if (IsFileInUse(filePath))
        {
            return new JsonResult(new { success = false, message = "file is in use" });
        }
        else
        {

            return new JsonResult(new { success = true, message = "File is not in use and can be accessed." });
        }
    }
    catch (Exception ex)
    {
        // Handle any unexpected errors
        return new JsonResult(new { success = false, message = ex.Message });
    }
}

private bool IsFileInUse(string filePath)
{
    try
    {
        using (FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
        {
            // File is not in use
        }
        return false;
    }
    catch (IOException)
    {
        // File is in use
        return true;
    }
}