메모리에서 파일을 업로드하는 코드가 있습니다.메모리에서 C# 파일 업로드
(디스크에서 파일 읽기) 방법 1을 사용하면 코드가 정상적으로 작동합니다. 테스트 서버에서 업로드 된 파일을 볼 수 있습니다.
방법 2 (원하는 방식으로)를 사용하면 메모리에있는 데이터를 사용하면 파일이 서버에 업로드되지 않습니다.
일부 라이브러리에서 파일을 만듭니다. 메모리 스트림에 있습니다. 디스크에 저장하고 메모리에 다시로드하고 싶지 않습니다.
//code for savigng memory to file
if (AudioStream != null && AudioStream.Length > 0)
{
string path = $"D:\\temp\\r_123.mp3";
FileStream fs = new FileStream(path, FileMode.Create);
AudioStream.Seek(0, SeekOrigin.Begin);
AudioStream.CopyTo(fs);
fs.Close();
}
using (Stream AudioStream = new MemoryStream())
{
// method 1 [working]
//load into memory from file
//using (FileStream fs = File.OpenRead(@"D:\Projects\T1.mp3"))
//{
// fs.CopyTo(AudioStream);
//}
//end of method 1
// method 2, not working
memory_stream.Seek(0, SeekOrigin.Begin);
memory_stream.CopyTo(AudioStream);
// end of method 2
AudioStream.Seek(0, SeekOrigin.Begin);
using (HttpClient httpClient = new HttpClient())
{
using (MultipartFormDataContent form = new MultipartFormDataContent())
{
using (var streamContent = new StreamContent(memory_stream))
{
using (var imageContent = new ByteArrayContent(streamContent.ReadAsByteArrayAsync().Result))
{
imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
form.Add(imageContent, "file", "cls.mp3");
var response = httpClient.PostAsync(to_url, form).Result;
}
}
}
}
}
이 문제를 해결하는 방법에 대한 어떤 생각 : 그리고 디스크에서 테스트 파일은 다음 코드에서 메모리에서 생성된다? 도와주세요!
미리 감사드립니다.
그리고 memorystream에서 새로운 memorystream으로 데이터를 복사 할 필요가 없습니다. –