ASP.Net'in yerleşik tesislerini kullanmak yerine HttpListener
. Ama bunu yapman gerekiyorsa, buradaki örnek bir koddur. Not: 1) <form>
ürününüzde enctype="multipart/form-data"
kullanıyorsunuz. 2) Bu kod sadece <input type="file" />
'u içeren bir formla kullanılmak üzere tasarlanmıştır, eğer diğer alanları veya birden fazla dosyayı göndermek istiyorsanız kodu değiştirmeniz gerekecektir. 3) Bu kavram/örnek kanıtı, hatalar olabilir ve özellikle esnek değildir.
static void Main(string[] args)
{
HttpListener listener = new HttpListener();
listener.Prefixes.Add("http://localhost:8080/ListenerTest/");
listener.Start();
HttpListenerContext context = listener.GetContext();
SaveFile(context.Request.ContentEncoding, GetBoundary(context.Request.ContentType), context.Request.InputStream);
context.Response.StatusCode = 200;
context.Response.ContentType = "text/html";
using (StreamWriter writer = new StreamWriter(context.Response.OutputStream, Encoding.UTF8))
writer.WriteLine("File Uploaded");
context.Response.Close();
listener.Stop();
}
private static String GetBoundary(String ctype)
{
return "--" + ctype.Split(';')[1].Split('=')[1];
}
private static void SaveFile(Encoding enc, String boundary, Stream input)
{
Byte[] boundaryBytes = enc.GetBytes(boundary);
Int32 boundaryLen = boundaryBytes.Length;
using (FileStream output = new FileStream("data", FileMode.Create, FileAccess.Write))
{
Byte[] buffer = new Byte[1024];
Int32 len = input.Read(buffer, 0, 1024);
Int32 startPos = -1;
// Find start boundary
while (true)
{
if (len == 0)
{
throw new Exception("Start Boundaray Not Found");
}
startPos = IndexOf(buffer, len, boundaryBytes);
if (startPos >= 0)
{
break;
}
else
{
Array.Copy(buffer, len - boundaryLen, buffer, 0, boundaryLen);
len = input.Read(buffer, boundaryLen, 1024 - boundaryLen);
}
}
// Skip four lines (Boundary, Content-Disposition, Content-Type, and a blank)
for (Int32 i = 0; i < 4; i++)
{
while (true)
{
if (len == 0)
{
throw new Exception("Preamble not Found.");
}
startPos = Array.IndexOf(buffer, enc.GetBytes("\n")[0], startPos);
if (startPos >= 0)
{
startPos++;
break;
}
else
{
len = input.Read(buffer, 0, 1024);
}
}
}
Array.Copy(buffer, startPos, buffer, 0, len - startPos);
len = len - startPos;
while (true)
{
Int32 endPos = IndexOf(buffer, len, boundaryBytes);
if (endPos >= 0)
{
if (endPos > 0) output.Write(buffer, 0, endPos-2);
break;
}
else if (len <= boundaryLen)
{
throw new Exception("End Boundaray Not Found");
}
else
{
output.Write(buffer, 0, len - boundaryLen);
Array.Copy(buffer, len - boundaryLen, buffer, 0, boundaryLen);
len = input.Read(buffer, boundaryLen, 1024 - boundaryLen) + boundaryLen;
}
}
}
}
private static Int32 IndexOf(Byte[] buffer, Int32 len, Byte[] boundaryBytes)
{
for (Int32 i = 0; i <= len - boundaryBytes.Length; i++)
{
Boolean match = true;
for (Int32 j = 0; j < boundaryBytes.Length && match; j++)
{
match = buffer[i + j] == boundaryBytes[j];
}
if (match)
{
return i;
}
}
return -1;
}
burada, daha iyi Yukarıdaki kod ne yaptığını anlamasına yardımcı gibi HTTP POST gövdesi göründüğünü olduğunu için: Alakasız başlıklarını bıraktım
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary9lcB0OZVXSqZLbmv
------WebKitFormBoundary9lcB0OZVXSqZLbmv
Content-Disposition: form-data; name="my_file"; filename="Test.txt"
Content-Type: text/plain
Test
------WebKitFormBoundary9lcB0OZVXSqZLbmv--
. Gördüğünüz gibi, başlangıç ve bitiş sınır dizilerini bulmak için taramak suretiyle vücudunuzu ayrıştırmanız ve dosyanızın içeriğinden önce gelen alt başlıkları daraltmanız gerekir. Ne yazık ki, StreamReader'ı ikili veri potansiyeli nedeniyle kullanamazsınız. Ayrıca talihsiz herhangi bir dosya Content-Length (istek için Content-Length üstbilgisi yoktur) gerçeği sınırlarını, alt başlıkları ve boşlukları da dahil olmak üzere vücudun toplam uzunluğunu belirtir.
yolu basitleştiriyorsun gibi pek görünüyor ben? web formunda enctype = "multipart/form-data" kullandığınız götürün İçeriği okuyorsunuz –