cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Update: Find information on Dropbox support during COVID-19 here
Close

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: 
Highlighted

Cannot upload Image file with Dropbox API in C# - uploading corrupt image instead

Explorer | Level 3

Hello.

I am trying to upload an image file into my dropbox account with the dropbox api. The image is being uploaded using this code, but it's a corrupt image.

Here is my code:

 

        public async Task UploadToDropbox()
        {
            using (var dbx = new DropboxClient(dbToken))
            {
                string srcFilePath = @"~\useruploads\";
                string srcFileName = "image1.png";
                string srcFile = srcFilePath + srcFileName;
                string targetFolder = "/uploads";
                string targetFileName = "userupload_" + DateTime.Now.ToString("yymmssfff") + ".png";
                                
                
                //byte[] readContent = Encoding.UTF8.GetBytes(srcFile); //uploading corrupt file
                //byte[] readContent = UTF8Encoding.UTF8.GetBytes(srcFile); //uploading corrupt file
                //var readContent = System.IO.File.ReadAllBytes(srcFile); //uploading nothing
                //byte[] readContent = System.IO.File.ReadAllBytes(srcFile); //uploading nothing

using (var mem = new MemoryStream(readContent)) { var updated = await dbx.Files.UploadAsync( targetFolder + "/" + targetFileName, WriteMode.Overwrite.Instance, body: mem); } } }

Thank you for any help.

1 Accepted Solution

Accepted Solutions
Highlighted

Re: Cannot upload Image file with Dropbox API in C# - uploading corrupt image instead

Dropboxer
Dropboxer

If you want to upload a local file, you can just do something like this:

using (var file = new FileStream(srcFile, FileMode.Open))
{
    var updated = await dbx.Files.UploadAsync(
        targetFolder + "/" + targetFileName,
        WriteMode.Overwrite.Instance,
        body: file);
}

You'll probably want to add some checking/exception handling to make sure the local file actually exists at that path though.

View solution in original post

2 Replies 2
Highlighted

Re: Cannot upload Image file with Dropbox API in C# - uploading corrupt image instead

Dropboxer
Dropboxer

If you want to upload a local file, you can just do something like this:

using (var file = new FileStream(srcFile, FileMode.Open))
{
    var updated = await dbx.Files.UploadAsync(
        targetFolder + "/" + targetFileName,
        WriteMode.Overwrite.Instance,
        body: file);
}

You'll probably want to add some checking/exception handling to make sure the local file actually exists at that path though.

View solution in original post

Highlighted

Re: Cannot upload Image file with Dropbox API in C# - uploading corrupt image instead

Explorer | Level 3

Unfortunately, with that change, the program isn't going into the using bracket. I tried it out with a try/finally and it seems to just go back to the main method after the "var file = new FileStream(srcFile, FileMode.Open)" line. Any idea why that's happening?

Edit: This was happening because the route was incorrect - your reply worked, thank you!

Work Smarter with Dropbox

The way we work is changing. Share and discover new ways to work smarter with Dropbox in our community.

Sound good? Let's get started.
Who's talking

Top contributors to this post

What do Dropbox user levels mean?
Need more support?