ファイルのアップロードするプログラムを作成しているのですが、
ファイルアップロードはできるのですがファイルが壊れてしまって開けない状態になってしまいます。
下記ソースでファイルはできるのですが壊れています。
よろしくお願いします。
var stream = Request.InputStream; //1mbづつアップされる。
var position = Int32.Parse(Request.Headers["ChunkPosition"]);
var bytes = ReadToEnd(InputStream);
if (bytes.Length > 0)
{
using (FileStream fs = new FileStream(_path, FileMode.Open, FileAccess.Write, FileShare.Write, bytes.Length, true))
{
fs.Seek(position, SeekOrigin.Begin);
fs.WriteAsync(bytes, 0, bytes.Length);
}
}
public static byte[] ReadToEnd(System.IO.Stream stream) { long originalPosition = 0; if (stream.CanSeek) { originalPosition = stream.Position; stream.Position = 0; } try { byte[] readBuffer = new byte[4096]; int totalBytesRead = 0; int bytesRead; while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0) { totalBytesRead += bytesRead; if (totalBytesRead == readBuffer.Length) { int nextByte = stream.ReadByte(); if (nextByte != -1) { byte[] temp = new byte[readBuffer.Length * 2]; Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length); Buffer.SetByte(temp, totalBytesRead, (byte)nextByte); readBuffer = temp; totalBytesRead++; } } } byte[] buffer = readBuffer; if (readBuffer.Length != totalBytesRead) { buffer = new byte[totalBytesRead]; Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead); } return buffer; } finally { if (stream.CanSeek) { stream.Position = originalPosition; } } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/19 03:41
2018/07/19 03:58
2018/07/19 04:10
2018/07/19 04:16
2018/07/19 04:24
2018/07/19 04:35
2018/07/19 05:08