मैं एक छवि यूआरएल कैसे प्राप्त करूं और उसे डिस्क पर कैसे लिखूं?


मैं एक यूआरएल से छवि प्राप्त करने की कोशिश कर रहा हूं और फिर बिना किसी सफलता के छवि को डिस्क पर लिख रहा हूं।
क्या कोई इसके लिए सहायता कर सकता है?

उदाहरण यूआरएल है:
https://static.tvmaze.com/uploads/images/medium_portrait/244/610854.jpg

वर्तमान कोड है:

try
			{
				using var client = new HttpClient();
				var getStream = client.GetStreamAsync(webURL);
				using var fileStream = new FileStream(imagePath,FileMode.Create,FileAccess.Write);
				return true;
			}

यह हमेशा एक खाली jpg छवि लिखता है।
हालाँकि, जब मैं सीधे यूआरएल निष्पादित करता हूं तो यह छवि दिखाता है।

मैंने क्या प्रयास किया है:

मैंने पुनर्प्राप्ति का उपयोग करने का प्रयास किया है जैसा कि मैं JSON डेटा के लिए करता हूँ। लेकिन वह काम नहीं करता.

समाधान 1

यहां बताया गया है कि मैं यह कैसे करता हूं:

WebClient wc = new WebClient();
outPath = Path.Combine(KnownFolders.Downloads.Path, $"{imageFileName}.{imageFileExtension}");
wc.DownloadFile(imageUrl, outPath);

समाधान 2

बस थूक उछालना, लेकिन ऐसा कुछ संभव होना चाहिए।

सी#
private HttpClient httpClient;
public async Task DownloadImageAndSaveAsync(string sourceFile, string outputFolder, string outputFileName)
{
  try
  {
    using Stream fileStream = await httpClient.GetStreamAsync(fileUrl);
    Directory.CreateDirectory(outputFolder);
    string path = Path.Combine(outputFolder, outputFileName);
    using FileStream outputFileStream = new FileStream(path, FileMode.CreateNew);
    await fileStream.CopyToAsync(outputFileStream);
  }
  catch (Exception ex)
  {
    // If you can do some exception handling, do it here...
  }
}

コメント

タイトルとURLをコピーしました