When reading a CSV file in DropBox using dotnet maui, the data becomes HTML instead of the contents of the CSV file.

康二郎 井上 60 Reputation points
2024-08-22T07:48:39.1266667+00:00

string fileUrl = "https://www.dropbox.com/scl/fi/sulYYYYYYYYY/XXXX.csv?dl=1";

string localPath = Path.Combine(FileSystem.Current.AppDataDirectory, "XXXX.csv");

System.Diagnostics.Debug.WriteLine($"Local file path: {localPath}");

try

{ // download of the file

var response = await client.GetAsync(fileUrl);

response.EnsureSuccessStatusCode();

//

var fileBytes = await response.Content.ReadAsByteArrayAsync();

//

await File.WriteAllBytesAsync(localPath, fileBytes);

Debug.WriteLine("The download was successful."); }

catch (Exception ex)

{

Debug.WriteLine("Download failed.");

Debug.WriteLine($"Error: {ex.Message}"); }

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,369 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,819 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 41,601 Reputation points Microsoft Vendor
    2024-08-23T02:20:59.12+00:00

    Hello,

    If you need to read files from DropBox or NetDisk, you need to use the API provided by the NetDisk service provider to realize this function.

    For Dropbox you can refer to the section on downloading files in the official .net API documentation provided by DropBox.

    //To download a file:
    
    async Task Download(DropboxClient dbx, string folder, string file)
    {
        using (var response = await dbx.Files.DownloadAsync(folder + "/" + file))
        {
            Console.WriteLine(await response.GetContentAsStringAsync());
        }
    }
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our [documentation][1] to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. 康二郎 井上 60 Reputation points
    2024-08-26T06:52:21.2266667+00:00

    I solved the problem.

    1. Sign in to DropBox and create an app with a suitable app name (I named it 'CSVFileApp'). Check the app in the Dropbox developer console Dropbox Developers' https://www.dropbox.com/developers/apps', click to open the app, open Permission type Scoped App, check files.content.write, check files.content.read, check file_requests.write, check file_requests.read with a gray check, check contacts.write, check contacts.read with a gray check, and click Submit to set the scope of this app.

    Go back to 'https://www.dropbox.com/developers/apps' and open the app you created again, generate an access token, and save it.

    From here, use dotnet maui to upload the necessary CSV files to this app. I tried to do this process manually, but it meant that I couldn't download anything later.

    However, the access token for downloading CSV from DropBox will change after 1-2 days and will no longer be usable, so I will withdraw from the free DropBox service.

    Thank you.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.