WebClient.Download question.

Asked 2 years ago, Updated 2 years ago, 79 views

I'm trying to download a file from Unity using WebClient.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Net;

public class FileDownload : MonoBehaviour {
    public string text;
    public Text logText;
    // // Use this for initialization
    void Start () {

    }

    // // Update is called once per frame
    void Update () {
        logText.text = text;
    }

    public void DownloadURL(){
        string downLink = "https://drive.google.com/open?id=0ByKk42PO-EvebmRGN0Y0V2ZSVmc";
        string filename = "C:/User/ISENDO/Desktop/Copy/deny.jpg";

        WebClient myWebClient = new WebClient ();

        try{
            myWebClient.DownloadFile(downLink,filename);        
        }

        catch(WebException error){
            text += error + "\n";
        }
    }
}

When I do this, I can't get the picture normally and I keep getting 111KB files.

What's the problem?

c# unity

2022-09-21 21:48

2 Answers

The URL you want to download is not the URL that only contains the image, but the URL of the web page that displays the image. I need a link that only lowers the ULR.

For example, if you change the image url to this, it will work well.

https://res.cloudinary.com/eightcruz/image/upload/v1490000448/banner_slack_2x_iegsyt.png

If you need an image server, http://cloudinary.com/ is recommended. After uploading the image here, you can get the URL that you can get right away.


2022-09-21 21:48

public static Image getImage(String url) {
            String defaultUrl = Basic.WEB_ROOT; // Default URL, modify if desired.
            String totalUrl = defaultUrl + url;

            // Download the image.
            byte[] data = new WebClient().DownloadData(totalUrl);
            MemoryStream ms = new MemoryStream(data);
            return Image.FromStream(ms);
        }

It's a method that I personally use.

And I don't think the URL you're using (Google Drive) will work because the image is not a direct link.


2022-09-21 21:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.