Unrecognized Element (Download Files)

Asked

Viewed 55 times

1

I am trying to make a code to download the file in a folder and returns me this error unrecognized element.

Code:

string pasta = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + @"\teste\";
download = new WebClient();
download.DownloadFileAsync(new Uri("https://drive.google.com/uc?authuser=download&export=download"), pasta+"Programa.exe");

What can it be?

1 answer

2


The Google does not allow downloads in this way by Google Drive.

To download files via Google Drive through code, you make a request HTTP GET authorized to the resource URL file and includes the query parameter alt = media. For example:

GET https://www.googleapis.com/drive/v3/files/0B9jNhSvVjoIVM3dKcGRKRmVIOVU?alt=media

Authorization: Bearer

Downloading the file requires the user to have at least read. Also, your app should be authorized with a scope which allows the reading of the contents of the file. For example, a application that uses the scope drive.readonly.metadata would not be authorized to download the contents of the file. Users with permission editing may restrict download by read-only users defining the field viewersCanCopyContent as true.

Files identified as abusive (malware, etc.) can only be downloaded by the owner. In addition, the query parameter acknowledledgeAbuse = true should be included to indicate that the user recognized the risk of downloading a potential malware. Their app should interactively warn user before using this query parameter.

I would recommend that you put your file on another site, such as Dropbox, Azure Blob or files.fm, where I’m not mistaken, you don’t need exotic code to allow you to download files like Google Drive.

However, if you wanted to continue with the Drive, in the link below you check how to do what you want.

Information collected from: Google Drive Apis

EDIT

I just passed on the wrong information and in the case of Dropbox, you also need to use a DropboxClient to download files + a Token generated by the API of Dropbox. That said, let’s get down to business.

1. Create a Dropbox account

Log in to Dropbox and create your account.

2. Create an App to access a Token

Log in to Dropbox Developers and create an App that will be used to generate a Token so we can download Dropbox files.

inserir a descrição da imagem aqui

On the screen that opens, select Dropbox API, then select Full Dropbox, define a name for your App and finally, click on Create App.

inserir a descrição da imagem aqui

3. Generate a Token

On the open screen, look for Generated access token and click on Generate. Once this is done, a Token will be generated. Save this Token in a secure location. It will be used to identify via code your account Dropbox.

inserir a descrição da imagem aqui

4. Create a folder in Dropbox

Create a folder in Dropbox and put the file you want to download in there. You can use the same folder to put any type of file you want to download.

inserir a descrição da imagem aqui

Once this is done, add the desired file(s) (s) into the created folder. In this case, I put a file .7z (zip) under the name of cv_2018.

inserir a descrição da imagem aqui

5. Create the code to download the file

private string _token = "token_gerado_nas_etapas_anteriores";

public Form1()
{
    InitializeComponent();

    btnDownload.Click += Download_Click;
}

/// <summary>
/// Click do botão para download do arquivo
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void Download_Click(object sender, EventArgs e)
{
    try
    {
        DropboxClient client;

        client = new DropboxClient(_token);
        string folder = "DownloadFiles";
        string file = "cv_2018.7z";
        string destinationFolder = @"C:\Test";

        await Download(client, folder, file, destinationFolder);
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Erro ao fazer download do arquivo: {ex.Message}");
    }
}

/// <summary>
/// Faz o download de um arquivo do Dropbox e salva localmente
/// </summary>
/// <param name="dbx">DropboxClient</param>
/// <param name="folder">Pasta onde se encontra o arquivo no Dropbox</param>
/// <param name="file">Nome do arquivo com a extensão no Dropbox</param>
/// <param name="destinationFolder">Pasta de destino no computador</param>
/// <returns></returns>
private async Task Download(DropboxClient dbx, string folder, string file, string destinationFolder)
{
    using (var response = await dbx.Files.DownloadAsync("/" + folder + "/" + file))
    {
        using (var fileStream = File.Create($"{destinationFolder}\\{file}"))
        {
            (await response.GetContentAsStreamAsync()).CopyTo(fileStream);
        }
    }
}
  • And how can I make a direct download link in Dropbox for example?

  • @Slinidy fooled me about this and Dropbox also requires a way to download files similar to Google. Soon I will be updating the reply with the information.

  • @Slinidy Reply updated. The steps have been tested and the code is working perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.