cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to check folder exist or not and how to upload image using URL using C#

How to check folder exist or not and how to upload image using URL using C#

JayPatel1992
Helpful | Level 5
Go to solution

Hello there, 
I am using Dropbox.Api. I want to check if folder exist or not. I tired using try and catch block but I think there should be a better way for doing this.
Here is my try catch block:

try
            {
                var isFolder = client.Files.GetMetadataAsync(path).Result.IsFolder;
            }
            catch (Exception e)
            {
                // TODO Auto-generated catch block
                if (e.InnerException.Message == "path/not_found/...")
                {
                  var folder = CreateFolder(client, path);
                }
            }

Also I want to upload image to dropbox using image URL.
Here is what I am doing, but didn't upload any image nor giving any error.

Please help me with this.

List<Images> fiveElements = Images.GetRange(0, 5);
            foreach (var image in fiveElements)
            {
                using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(image.ImageURL)))
                {
                    string fileName = image.ImageURL;
                    var response = await client.Files.UploadAsync(path + "/" + fileName, WriteMode.Overwrite.Instance, body: stream);
                    Console.WriteLine("Uploaded Id {0} Rev {1}", response.Id, response.Rev);
                }
            }
2 Accepted Solutions

Accepted Solutions

JayPatel1992
Helpful | Level 5
Go to solution

Didn't get much about SvaeUrlAsync but I used this and it worked perfectly.

List<Images> fiveElements = Images.GetRange(0, 5);
            foreach (var image in fiveElements)
            {
                WebClient webclient = new WebClient();
                var stream = new MemoryStream(webclient.DownloadData(image.ImageURL));
                var fileName = "Test1.jpg";
                var response = await client.Files.UploadAsync(path + "/" + fileName, WriteMode.Overwrite.Instance, true, body: stream);
            }

And for try catch I used your code but not working well as it throws me out of this method and goes to catch(Exception e) of parent method.

try
            {
                await client.Files.GetMetadataAsync(path);
            }
            catch (ApiException<Dropbox.Api.Files.GetMetadataError> e)
            {
                if (e.ErrorResponse.IsPath && e.ErrorResponse.AsPath.Value.IsNotFound)
                {
                    Console.WriteLine("Nothing found at path.");
                }
                else
                {
                    // different issue; handle as desired
                    Console.WriteLine(e);
                }
            }

Please let me know where I am going wrong.
Thank you.

View solution in original post

JayPatel1992
Helpful | Level 5
Go to solution

Thank you so much for the help. Using await and removing result worked for me.

try
            {
                await client.Files.GetMetadataAsync(path);
            }
            catch (ApiException<Dropbox.Api.Files.GetMetadataError> e)
            {
                if (e.ErrorResponse.IsPath && e.ErrorResponse.AsPath.Value.IsNotFound)
                {
                    Console.WriteLine("Nothing found at path.");
                }
                else
                {
                    // different issue; handle as desired
                    Console.WriteLine(e);
                }
            }

View solution in original post

6 Replies 6

Greg-DB
Dropbox Staff
Go to solution

Using the GetMetadataAsync method is the right way to check for the existence of something at a particular path. You're correct that there is a more native way to to the error handling though. For example:

 

catch (ApiException<Dropbox.Api.Files.GetMetadataError> e)
{
    if (e.ErrorResponse.IsPath && e.ErrorResponse.AsPath.Value.IsNotFound) {
        Console.WriteLine("Nothing found at path.");
    } else {
        // different issue; handle as desired
        Console.WriteLine(e);
    }
}

For your uploading code, it appears you're getting the bytes for the URL string itself, instead of downloading the actual data located at that URL. You should update your code to download the actual bytes, and then pass them to the Dropbox upload method.

 

Alternatively, as long as the URL is publicly accessible, you can pass the URL directly to the SaveUrlAsync method to have Dropbox do the downloading for you:

 

https://dropbox.github.io/dropbox-sdk-dotnet/html/M_Dropbox_Api_Files_Routes_FilesUserRoutes_SaveUrl...

JayPatel1992
Helpful | Level 5
Go to solution

Thank you @Greg K. for the help. That's really quick.

 

As I am new to this coding stuff, it would be good if I have example for FilesUserRoutes.SaveUrlAsync Method which explains in detail.

 

And does the try block is good ? because I tired your catch block and it thows me out of the method and goes into catch(Exception e) of parent method.

 

 

Greg-DB
Dropbox Staff
Go to solution

I don't have a sample available for that unfortunately. The basic idea is to call SaveUrlAsync with the 'path' for where you want to save the file, and the 'url' for the hosted file data to save. Then, if the SaveUrlResult gives you an AsyncJobId, call SaveUrlCheckJobStatusAsync ocasionally to check on the job status. 

 

Please give that a try and let me know if something isn't working as expected.

 

And yes, using the try/catch pattern is a good way to do that existence check. 

JayPatel1992
Helpful | Level 5
Go to solution

Didn't get much about SvaeUrlAsync but I used this and it worked perfectly.

List<Images> fiveElements = Images.GetRange(0, 5);
            foreach (var image in fiveElements)
            {
                WebClient webclient = new WebClient();
                var stream = new MemoryStream(webclient.DownloadData(image.ImageURL));
                var fileName = "Test1.jpg";
                var response = await client.Files.UploadAsync(path + "/" + fileName, WriteMode.Overwrite.Instance, true, body: stream);
            }

And for try catch I used your code but not working well as it throws me out of this method and goes to catch(Exception e) of parent method.

try
            {
                await client.Files.GetMetadataAsync(path);
            }
            catch (ApiException<Dropbox.Api.Files.GetMetadataError> e)
            {
                if (e.ErrorResponse.IsPath && e.ErrorResponse.AsPath.Value.IsNotFound)
                {
                    Console.WriteLine("Nothing found at path.");
                }
                else
                {
                    // different issue; handle as desired
                    Console.WriteLine(e);
                }
            }

Please let me know where I am going wrong.
Thank you.

Greg-DB
Dropbox Staff
Go to solution
I think that's because you're using '.Result' instead of 'await'. That's more of a general C# exception handling matter though, so I can't provide much insight there. You'll probably need to change how you structure your exception handling, or switch to using 'await'.

JayPatel1992
Helpful | Level 5
Go to solution

Thank you so much for the help. Using await and removing result worked for me.

try
            {
                await client.Files.GetMetadataAsync(path);
            }
            catch (ApiException<Dropbox.Api.Files.GetMetadataError> e)
            {
                if (e.ErrorResponse.IsPath && e.ErrorResponse.AsPath.Value.IsNotFound)
                {
                    Console.WriteLine("Nothing found at path.");
                }
                else
                {
                    // different issue; handle as desired
                    Console.WriteLine(e);
                }
            }
Need more support?
Who's talking

Top contributors to this post

  • User avatar
    JayPatel1992 Helpful | Level 5
  • User avatar
    Greg-DB Dropbox Staff
What do Dropbox user levels mean?