简体   繁体   中英

ASP.NET Dropbox API uploading .pdf files

I have tried uploading a pdf file using the API's UploadAsync() mentioned here: https://www.dropbox.com/developers/documentation/dotnet#tutorial

the file gets uploaded but it becomes corrupted and can't be opened, I read the content of the pdf file using

System.IO.File.ReadAllText(fileUri);

and then upload the file via awaiting this function that returns a shareable link to the file on dropbox:

    public static async Task<string> UploadFile(string folder, string fileName, string content)
    {
        using (var mem = new MemoryStream(Encoding.UTF8.GetBytes(content)))
        {
            FileMetadata updated = await dbx.Files.UploadAsync(
            folder + "/" + fileName,
            WriteMode.Overwrite.Instance,
            body: mem);

            var arg1 = new Dropbox.Api.Sharing.CreateSharedLinkWithSettingsArg(folder + "/" + fileName);
            var share = await dbx.Sharing.CreateSharedLinkWithSettingsAsync(arg1);
            return share.Url;
        }
    }

I figured out it was related to the encoding as the uploaded file contained different characters than the original, but I couldn't find the correct encoding, or if there is a better approach ?

Thanks in advance.

The System.IO.File.ReadAllText(fileUri) reads the bytes of the files and converts them to a string. Then writing the string back using Encoding.UTF8 may change the content.

Please skip this transformation and use the bytes directly. Eg

var content = System.IO.File.ReadAllBytes(fileUri);

And change the function from string content to byte[] content .

public static async Task<string> UploadFile(string folder, string fileName, byte[] content)
{
    using (var mem = new MemoryStream(content))
    {
        FileMetadata updated = await dbx.Files.UploadAsync(
        folder + "/" + fileName,
        WriteMode.Overwrite.Instance,
        body: mem);

        var arg1 = new Dropbox.Api.Sharing.CreateSharedLinkWithSettingsArg(folder + "/" + fileName);
        var share = await dbx.Sharing.CreateSharedLinkWithSettingsAsync(arg1);
        return share.Url;
    }
}

This solution will still copy the whole file content to memory. It is more efficient regarding memory consumption to read directly from the file.

public static async Task<string> UploadFile(string folder, string fileName, string fileUri)
{
    using (var mem = new FileStream(fileUri, FileMode.Open, FileAccess.Read))
    {
        FileMetadata updated = await dbx.Files.UploadAsync(
        folder + "/" + fileName,
        WriteMode.Overwrite.Instance,
        body: mem);

        var arg1 = new Dropbox.Api.Sharing.CreateSharedLinkWithSettingsArg(folder + "/" + fileName);
        var share = await dbx.Sharing.CreateSharedLinkWithSettingsAsync(arg1);
        return share.Url;
    }
}

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