Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I coded the handler:

using System;
using System.Net;
using System.Web;

namespace Teste
{
    public class Exemplo : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Clear();
            context.Response.StatusCode = (int)HttpStatusCode.OK;

            string boundary = CreateFormDataBoundary();
            context.Response.ContentType = "multipart/form-data; boundary=" + boundary;
            //context.Response.ContentType = "plain/text";
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;

            context.Response.Headers.Add("X-data", "21-08-2017");
            context.Response.Headers.Add("X-numberfiles", "2");
            context.Response.Headers.Add("X-Id", "45625625EFA22AD");

            string[] files = new string[] { @"C:ExemploFicheirosFich1.txt", @"C:ExemploFicheirosFich2.txt" };

            string result = "";
            foreach (string f in files)
            {
                result = result + WriteFilePart(boundary, f);
            }
            result = result + Environment.NewLine + "--" + boundary;
            context.Response.Write(result);
        }
        private string CreateFormDataBoundary()
        {
            return "---------------------------" + DateTime.Now.Ticks.ToString("x");
        }
        private string WriteFilePart(string boundary, string FileName)
        {
            //Write boundary with file multipart header.
            string Tosend = "";
            Tosend = Tosend + Environment.NewLine + "--" + boundary + Environment.NewLine;
            Tosend = Tosend + "Content-Type: " + "text/plain" + "" + Environment.NewLine;
            Tosend = Tosend + "Content-Location: " + FileName + "" + Environment.NewLine;
            Tosend = Tosend + "Content-Disposition: attachment; filename="" + FileName + """ + Environment.NewLine;
            Tosend = Tosend + "Content-ID: " + FileName + "" + Environment.NewLine;
            Tosend = Tosend + Environment.NewLine;

            Tosend = Tosend + Environment.NewLine;
            var reader = new System.IO.StreamReader(FileName);
            var data = reader.ReadToEnd();
            var dataBinary = System.Text.Encoding.UTF8.GetBytes(data);
            Tosend = Tosend + System.Text.Encoding.UTF8.GetString(dataBinary);
            Tosend = Tosend + Environment.NewLine;

            return Tosend;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

If i use context.Response.ContentType = "multipart/form-data; boundary=" + boundary; which is what is supposed to happen i get fiddler response (fiddler says that there is no response body):

    HTTP/1.1 200 OK
Cache-Control: private
Content-Type: multipart/form-data; boundary=---------------------------8d5bf0fdc7ec8f6; charset=utf-8
Server: Microsoft-IIS/10.0
X-data: 21-08-2017
X-numberfiles: 2
X-Id: 45625625EFA22AD
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcQ2VydGlmaWNhw6fDo29TQVBcQ2VydGlmaWNhY2FvU0FQX3YxXFNBUENlcnRcVGVzdGUxXEV4ZW1wbG8uYXNoeA==?=
X-Powered-By: ASP.NET
Date: Mon, 21 May 2018 10:41:58 GMT
Content-Length: 581

If i use context.Response.ContentType = "plain/text" i get fiddler response that would expect to receive with multipart:

    HTTP/1.1 200 OK
Cache-Control: private
Content-Type: plain/text; charset=utf-8
Server: Microsoft-IIS/10.0
X-data: 21-08-2017
X-numberfiles: 2
X-Id: 45625625EFA22AD
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcQ2VydGlmaWNhw6fDo29TQVBcQ2VydGlmaWNhY2FvU0FQX3YxXFNBUENlcnRcVGVzdGUxXEV4ZW1wbG8uYXNoeA==?=
X-Powered-By: ASP.NET
Date: Mon, 21 May 2018 10:40:37 GMT
Content-Length: 581


-----------------------------8d5bf0fac25e36f
Content-Type: text/plain
Content-Location: C:ExemploFicheirosFich1.txt
Content-Disposition: attachment; filename="C:ExemploFicheirosFich1.txt"
Content-ID: C:ExemploFicheirosFich1.txt


Nome:joao
Idade:53

-----------------------------8d5bf0fac25e36f
Content-Type: text/plain
Content-Location: C:ExemploFicheirosFich2.txt
Content-Disposition: attachment; filename="C:ExemploFicheirosFich2.txt"
Content-ID: C:ExemploFicheirosFich2.txt


Dados Adicionais

-----------------------------8d5bf0fac25e36f

The specification clearly says that the response should be multipart/form-data. Why can't i obtain the body when use multipart/form-data? The final result should be the response obtained with plain/text but with context.Response.ContentType = "multipart/form-data;. What am i doing wrong?

Tkx

When i tried to use System.Net.Http.MultipartFormDataContent as content in context.Response.Write(result); fiddler shows System.Net.Http.MultipartFormDataContent as the body

Following Liam suggestion changed the code for:

        public void ProcessRequest(HttpContext context)
    {
        context.Response.Clear();
        context.Response.StatusCode = (int)HttpStatusCode.OK;

        string boundary = CreateFormDataBoundary();
        context.Response.ContentType = "multipart/mixed; boundary=" + boundary;
        //context.Response.ContentType = "plain/text";
        context.Response.ContentEncoding = System.Text.Encoding.UTF8;

        context.Response.Headers.Add("X-data", "21-08-2017");
        context.Response.Headers.Add("X-numberfiles", "2");
        context.Response.Headers.Add("X-Id", "45625625EFA22AD");

        string[] files = new string[] { @"C:ExemploFicheirosFich1.txt", @"C:ExemploFicheirosFich2.txt" };

        //string result = "";
        //foreach (string f in files)
        //{
        //    result = result + WriteFilePart(boundary, f);
        //}
        //result = result + Environment.NewLine + "--" + boundary;
        //StringContent a = (result);

        //HttpContent stringContent = new StringContent(result);
        ProcessRequestM(context);
        //context.Response.Write();
    }
    public void ProcessRequestM(HttpContext context)
    {
        Stream fileStream1 = File.OpenRead(@"C:ExemploFicheirosFich1.txt");
        Stream fileStream2 = File.OpenRead(@"C:ExemploFicheirosFich2.txt");

        HttpContent stringContent = new StringContent("parametro");
        HttpContent fileStreamContent1 = new StreamContent(fileStream1);
        HttpContent fileStreamContent2 = new StreamContent(fileStream2);
        //HttpContent bytesContent = new ByteArrayContent(paramFileBytes);
        using (var client = new HttpClient())
        using (var formData = new MultipartFormDataContent())
        {
            formData.Add(stringContent, "param1", "param1");
            formData.Add(fileStreamContent1, "file1", "file1");
            formData.Add(stringContent, "param2", "param2");
            formData.Add(stringContent, "param2", "param2");
            formData.Add(fileStreamContent2, "file2", "file2");
            context.Response.Write(formData);
        }
    }

Fiddler response:

    HTTP/1.1 200 OK
Cache-Control: private
Content-Type: multipart/mixed; boundary=---------------------------8d5bf354c77d23a; charset=utf-8
Server: Microsoft-IIS/10.0
X-data: 21-08-2017
X-numberfiles: 2
X-Id: 45625625EFA22AD
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcQ2VydGlmaWNhw6fDo29TQVBcQ2VydGlmaWNhY2FvU0FQX3YxXFNBUENlcnRcVGVzdGUxXEV4ZW1wbG8uYXNoeA==?=
X-Powered-By: ASP.NET
Date: Mon, 21 May 2018 15:09:57 GMT
Content-Length: 40

System.Net.Http.MultipartFormDataContent

Getting desperate

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
187 views
Welcome To Ask or Share your Answers For Others

1 Answer

What i was unable to explain is that i am the server. I only see the request i do not create it so the methods sugested do not apply. How i did it:

        public resp(HttpContext context)
    {
        context.Response.Clear();
        context.Response.StatusCode = (int)HttpStatusCode.OK;

        string formDataBoundary = String.Format("----------{0:N}", DateTime.Now.Ticks.ToString("x"));
        context.Response.ContentType = "multipart/form-data; boundary=" + formDataBoundary;
        context.Response.ContentEncoding = System.Text.Encoding.UTF8;

        context.Response.Headers.Add("X-data", "21-08-2017");
        context.Response.Headers.Add("X-numberfiles", "2");
        context.Response.Headers.Add("X-Id", "45625625EFA22AD");

        string[] files = new string[] { @"C:ExemploFicheirosFich1.txt", @"C:ExemploFicheirosFich2.txt" };
        Stream memStream = new MemoryStream();
        var boundarybytes = System.Text.Encoding.UTF8.GetBytes("--" + formDataBoundary + "
");
        var endBoundaryBytes = System.Text.Encoding.UTF8.GetBytes("
--" + formDataBoundary + "--");

        foreach (var FileName in files)
        {
            WriteFilePart(context, FileName, boundarybytes, memStream);
            boundarybytes = System.Text.Encoding.UTF8.GetBytes("
--" + formDataBoundary + "
");
        }

        memStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);

        using (Stream ResponseStream = context.Response.OutputStream)
        {
            memStream.Position = 0;
            byte[] tempBuffer = new byte[memStream.Length];
            memStream.Read(tempBuffer, 0, tempBuffer.Length);
            memStream.Close();
            ResponseStream.Write(tempBuffer, 0, tempBuffer.Length);
        }


    }
    private string WriteFilePart(HttpContext context, string FileName, byte[] boundarybytes, Stream memStream)
        {

            string Tosend = "";
            Tosend = Tosend + "Content-Type: " + "text/plain" + "" + Environment.NewLine;
            Tosend = Tosend + "Content-Location: " + FileName + "" + Environment.NewLine;
            Tosend = Tosend + "Content-Disposition: attachment; filename="" + FileName + """ + Environment.NewLine;
            Tosend = Tosend + "Content-ID: " + FileName + "" + Environment.NewLine;
            Tosend = Tosend + Environment.NewLine;

            memStream.Write(boundarybytes, 0, boundarybytes.Length);
            var headerbytes = System.Text.Encoding.UTF8.GetBytes(Tosend);
            memStream.Write(headerbytes, 0, headerbytes.Length);

            var filebyte = MultipartParser.Parse.FileToByteArray(FileName);  //converts file to byte[]
            memStream.Write(filebyte, 0, filebyte.Length);

        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...