❔ How do we download an image from url in NET 6?
When I search it on google, all the answers are outdated.
Then I need to put it into my wwwroot folder too.
37 Replies
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.
the code I gave is correct right?
For getting the bytes of the image, yeah
this one is giving me an error
File.WriteAllBytes("deneme", bytes);
CS0119
And what is CS0119?
sadly it's all turkish, i will try to translate it now
"it's not an available function in this context"
Show the whole piece of code, then
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.
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
it's just the order 😄 the function itself is giving me the error warning
i will fix it now
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
Ahnah
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 bythat worked
So might wanna use that
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?
Not sure tbh
haha, guess i will try and see
I don't really store any files in the filesystem, it all goes to a CDN
it's localhost, would that be fine?
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
hmm, i am gonna try right now, but what did you mean by code clean up?
seemed fine to me
can just be
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
oh nice, I forgot about that first one
The use of null-forgiving operator also tells me you should rather have a null-check there
yeah, that is necessary i think
pic can give null
("pic", out string? pPicUrl)
okay got it
would be safer done as
or some such
out var
will doi see
Also, not sure about
return new JsonResult(Ok());
I think just return Ok()
would be enoughit's ajax
let me try
nah, gives an error
Of course
Task<JsonResult>
would then be Task<IActionResult>
we could do this in minimal api i think
does javascript accept I action result?
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 worriesokay, I am gonna remaster it tomorrow. First I need to download that file.
damn, speaking english gives a whole new world to a person.
It sure does lol
It is the lingua franca of the modern world after all. Doubly so in tech
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.
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.