PDA

View Full Version : ذخیرة فایل



Happy_davood
چهارشنبه 07 اردیبهشت 1384, 21:01 عصر
سلام
من فایلی رو که کاربر Upload کرده با روش زیر بصورت Stream درآوردم :

CurrentFile = MyHttpFilesCollection[iFilesCount];
System.IO.Stream MyStream = CurrentFile.InputStream;
و بعد انجام یه سری کار بر روی این Stream می خوام تا اون رو بصورت فایل رو Server ذخیره کنم ولی چطوری نمیدونم .
البته قبلاً فایل آپلود شده رو با متد CurrentFile.SaveAs ذخیره می کردم ولی الان که تبدیل به Stream شده نمیدونم چکار باید بکنم .
ممنونم .

Happy_davood
چهارشنبه 07 اردیبهشت 1384, 22:09 عصر
پیدا کردم :

// readStream is the stream you need to read
// writeStream is the stream you want to write to
private void ReadWriteStream(Stream readStream, Stream writeStream)
{
int Length = 256;
Byte [] buffer = new Byte[Length];
int bytesRead = readStream.Read(buffer,0,Length);
// write the required bytes
while( bytesRead > 0 )
{
writeStream.Write(buffer,0,bytesRead);
bytesRead = readStream.Read(buffer,0,Length);
}
readStream.Close();
writeStream.Close();
}

فراخوانی:

string saveTo = "some path to save"
// create a write stream
FileStream writeStream = new FileStream(saveTo, FileMode.Create, FileAccess.Write);
// write to the stream
ReadWriteStream(readStream,writeStream);