私は以前作った非同期のファイルアップローダをデバッグしようとしていますが、これはもう機能していません。HTML5非同期ファイルのアップロード、アップロードされたストリームは常に無効です
サーバーが受信しているストリームは、実際には保存されたファイル(画像)を開くことができません。
デバッグを簡略化するために、2つのメインファイル、フォームフィールドのHTMLファイルとASP.NETハンドラを備えた新しいASP.NETプロジェクトをセットアップしました。
ここのコードは非常に些細なものですが、私はまだ運がありません!それは私がコードを持って、他の誰かを助けた場合
using System;
using System.Collections.Generic;
using System.Web.Extensions;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;
namespace MultipleFileUploadTest
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Handler1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var stream = context.Request.InputStream;
MemoryStream memoryStream;
ReadFully(stream, out memoryStream);
Byte[] ba = memoryStream.ToArray();
var path = @"C:\Users\giuseppe.JHP\Desktop\Image upload test\uploaded.gif";
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
{
fs.Write(ba, 0, ba.Length);
}
//DEBUGGING CODE
//I'm opening the same file that was originally picked by the input form field and I'm now comparing the original file with the one received within the context stream. They always differ!
Byte[] ba2 = File.ReadAllBytes(@"C:\Users\giuseppe.JHP\Desktop\Image upload test\a.gif");
//equal evaluates always to false
bool equal = ba.Length == ba2.Length;
if (equal)
{
for (var i = 0; i < ba2.Length; i++)
{
if (ba[i] != ba2[i])
{
equal = false;
i = ba2.Length;
}
}
}
//equal is always false
//if (!equal)
//{
// throw Exception("Stream is not valid");
//}
//The code below will throw a Parameter is invalid exception
//System.Drawing.Image mediaObject = System.Drawing.Image.FromStream(memoryStream);
memoryStream.Close();
}
public static void ReadFully(Stream input, out MemoryStream ms)
{
ms = new MemoryStream();
byte[] buffer = new byte[16 * 1024];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
これは、Ctrl + K + DがVSで行うことですが、私はそれをさらに正式にフォーマットするのに多くの時間を費やしていません! –
@llnk C#用の新しい行はかなり標準です。私はいつも「アクセント付きで」コードを書かないように教えられていました。ジュゼッペは実際にはここではかなり標準的なコード規約に従っています。 – Crisfole