C
C#2y ago
İrşat

❔ How do we download an image from url in NET 6?

When I search it on google, all the answers are outdated.
var res = await _httpClient.GetAsync(picUrl);
byte[] bytes = await res.Content.ReadAsByteArrayAsync();
var res = await _httpClient.GetAsync(picUrl);
byte[] bytes = await res.Content.ReadAsByteArrayAsync();
Then I need to put it into my wwwroot folder too.
37 Replies
Angius
Angius2y ago
File.WriteAllBytes(String, Byte[]) Method (System.IO)
Creates a new file, writes the specified byte array to the file, and then closes the file. If the target file already exists, it is overwritten.
İrşat
İrşat2y ago
the code I gave is correct right?
Angius
Angius2y ago
For getting the bytes of the image, yeah
İrşat
İrşat2y ago
this one is giving me an error File.WriteAllBytes("deneme", bytes); CS0119
Angius
Angius2y ago
And what is CS0119?
İrşat
İrşat2y ago
sadly it's all turkish, i will try to translate it now "it's not an available function in this context"
Angius
Angius2y ago
Show the whole piece of code, then
İrşat
İrşat2y ago
[HttpPost]
public async Task<JsonResult> GoogleSignin([FromBody] AuthToken googleToken)
{
var apiResponse = await _httpClient.PostAsJsonAsync("Auth/Signin-External", googleToken.token);
string statusCode = apiResponse.StatusCode.ToString();

if (statusCode == "OK")
{
var dictResult = await getDictFromResponse(apiResponse);
string? newToken;
string? pPicUrl;
dictResult!.TryGetValue("jwt", out newToken);
dictResult!.TryGetValue("pic", out pPicUrl);
if (newToken != null) CreateUserSession(newToken);
if(pPicUrl != null && pPicUrl != "none"){
pPicUrl = pPicUrl.Replace("https://lh3.googleusercontent.com/ogw/","");

var res = await _httpClient.GetAsync(pPicUrl);
byte[] bytes = await res.Content.ReadAsByteArrayAsync();
File.WriteAllBytes("deneme", bytes);
}

return new JsonResult(Ok());
} else blabla
}
[HttpPost]
public async Task<JsonResult> GoogleSignin([FromBody] AuthToken googleToken)
{
var apiResponse = await _httpClient.PostAsJsonAsync("Auth/Signin-External", googleToken.token);
string statusCode = apiResponse.StatusCode.ToString();

if (statusCode == "OK")
{
var dictResult = await getDictFromResponse(apiResponse);
string? newToken;
string? pPicUrl;
dictResult!.TryGetValue("jwt", out newToken);
dictResult!.TryGetValue("pic", out pPicUrl);
if (newToken != null) CreateUserSession(newToken);
if(pPicUrl != null && pPicUrl != "none"){
pPicUrl = pPicUrl.Replace("https://lh3.googleusercontent.com/ogw/","");

var res = await _httpClient.GetAsync(pPicUrl);
byte[] bytes = await res.Content.ReadAsByteArrayAsync();
File.WriteAllBytes("deneme", bytes);
}

return new JsonResult(Ok());
} else blabla
}
I am trying to get the profile picture from google auth i can get the urls obviously now I will check if the file is in my storage, if it's not then I will download it.
Angius
Angius2y ago
Do you even get any bytes in your byte array? Seeing how you replace the whole chunk of the URL with an empty string But besides that, and besides the code having a couple of places where it could be cleaned up, it seems fine to me
İrşat
İrşat2y ago
it's just the order 😄 the function itself is giving me the error warning i will fix it now
Angius
Angius2y ago
Ah, huh... In this class, do you have a method, property, or something that's also named File? If so, then that might be the reason for CS0119 Ah
İrşat
İrşat2y ago
nah
Angius
Angius2y ago
Controller.File in the parent class for the controller So just use a fully-qualified name and it should work System.IO.File.WriteAllBytes() I think there's also an async version, by the by
İrşat
İrşat2y ago
that worked
Angius
Angius2y ago
So might wanna use that
İrşat
İrşat2y ago
await System.IO.File.WriteAllBytesAsync("deneme", bytes); now, what about this deneme? will it create a subfolder for wwwroot? and put the image inside of it?
Angius
Angius2y ago
Not sure tbh
İrşat
İrşat2y ago
haha, guess i will try and see
Angius
Angius2y ago
I don't really store any files in the filesystem, it all goes to a CDN
İrşat
İrşat2y ago
it's localhost, would that be fine?
Angius
Angius2y ago
Yeah, it should be fine Not sure if it'll be downloaded to wwwroot or not, but it won't crash the app or anything
İrşat
İrşat2y ago
hmm, i am gonna try right now, but what did you mean by code clean up? seemed fine to me
Angius
Angius2y ago
string? newToken;
string? pPicUrl;
dictResult!.TryGetValue("jwt", out newToken);
dictResult!.TryGetValue("pic", out pPicUrl);
string? newToken;
string? pPicUrl;
dictResult!.TryGetValue("jwt", out newToken);
dictResult!.TryGetValue("pic", out pPicUrl);
can just be
dictResult!.TryGetValue("jwt", out var newToken);
dictResult!.TryGetValue("pic", out var pPicUrl);
dictResult!.TryGetValue("jwt", out var newToken);
dictResult!.TryGetValue("pic", out var pPicUrl);
for example Also, instead of turning the status code to a string and checking that string... just check the status code apiResponse.StatusCode == StatusCode.OK
İrşat
İrşat2y ago
oh nice, I forgot about that first one
Angius
Angius2y ago
The use of null-forgiving operator also tells me you should rather have a null-check there
İrşat
İrşat2y ago
yeah, that is necessary i think pic can give null ("pic", out string? pPicUrl) okay got it
Angius
Angius2y ago
var dictResult = await getDictFromResponse(apiResponse);
dictResult!.TryGetValue("jwt", out var newToken);
var dictResult = await getDictFromResponse(apiResponse);
dictResult!.TryGetValue("jwt", out var newToken);
would be safer done as
var dictResult = await getDictFromResponse(apiResponse);

if (dictResult is not {} dr) return NotFound();

dr.TryGetValue("jwt", out var newToken);
var dictResult = await getDictFromResponse(apiResponse);

if (dictResult is not {} dr) return NotFound();

dr.TryGetValue("jwt", out var newToken);
or some such out var will do
İrşat
İrşat2y ago
i see
Angius
Angius2y ago
Also, not sure about return new JsonResult(Ok()); I think just return Ok() would be enough
İrşat
İrşat2y ago
it's ajax let me try nah, gives an error
Angius
Angius2y ago
Of course Task<JsonResult> would then be Task<IActionResult>
İrşat
İrşat2y ago
we could do this in minimal api i think does javascript accept I action result?
Angius
Angius2y ago
It still returns JSON by default Javascript has no concept of IActionResult Javascript receives JSON Which ASP returns Ok(), NotFound(), Unauthorized(), all of that returns stuff understandable by JS So no worries
İrşat
İrşat2y ago
okay, I am gonna remaster it tomorrow. First I need to download that file. damn, speaking english gives a whole new world to a person.
Angius
Angius2y ago
It sure does lol It is the lingua franca of the modern world after all. Doubly so in tech
İrşat
İrşat2y ago
Yeah this one worked
await System.IO.File.WriteAllBytesAsync($"wwwroot/images/users/{pPicUrl}.png", bytes);
didn't work quite a while with deneme because it wants an existing folder.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.