Coder Social home page Coder Social logo

Can not visit WebAPI about httplib HOT 2 CLOSED

wlfsky avatar wlfsky commented on June 29, 2024
Can not visit WebAPI

from httplib.

Comments (2)

j6mes avatar j6mes commented on June 29, 2024

from httplib.

wlfsky avatar wlfsky commented on June 29, 2024

webapi code like this:
vs2017, iis10 on win10 64x, dotnet 4.6, mvc 5.2.3.0

class ValuesController : ApiController {
        [HttpPost]
        public async Task<Dictionary<string, string>> Post(int id = 0)
        {
            // if MultipartBodyProvider.cs string.Format("multipart/form-data, boundary={0}", boundary);
            // The following code will not pass 
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }
            Dictionary<string, string> dic = new Dictionary<string, string>();
            string root = HttpContext.Current.Server.MapPath("~/App_Data");
            var provider = new MultipartFormDataStreamProvider(root);
            try
            {
                // run to this code will be throw error "Unexpected end of MIME multipart stream. MIME multipart message is not complete"
                await Request.Content.ReadAsMultipartAsync(provider);

                foreach (MultipartFileData file in provider.FileData)
                {
                    Trace.WriteLine(file.Headers.ContentDisposition.FileName);
                    Trace.WriteLine("Server file path: "+file.LocalFileName);
                }
                foreach (var key in provider.FormData.AllKeys)
                {
                    dic.Add(key, provider.FormData[key]);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dic;
        }
}

the post code

Http.Post("http://localhost:90/api/values/1").Upload(files:
                new[] {
                        new NamedFileStream("zzz.zip", @"F:\zzz.zip", "application/octet-stream", File.OpenRead(@"F:\zzz.zip"))
            }).OnSuccess((s) =>
            {
                WriteLine("--success--");
                WriteLine(s);
            }).OnFail((s) =>
            {
                WriteLine("--fail--");
                WriteLine(s);
            }).Go();

normal form post will success

<form action="http://localhost:90/Api/Values/1" method="post" enctype="multipart/form-data">
    <input name="id" value="1" />
    <input id="d" type="file" src="F:\work\11.xls" name="d"/>
    <input id="e" type="file" src="F:\work\22.xls" name="e"/>
    <input type="submit" value="tj" />
</form>

thanks !

from httplib.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.