Uploading file from Flex to WCF REST Stream issues (how to decode multipart form post in REST WS)

前端 未结 3 808
闹比i
闹比i 2020-12-30 16:19

The system is a Flex application communicating with a WCF REST web service. I am trying to upload a file from the Flex application to the server and am running into some is

相关标签:
3条回答
  • 2020-12-30 16:28

    Thanks to Anthony over at http://antscode.blogspot.com/ for the multipart parser that works great (for images, txt files, etc).

    http://antscode.blogspot.com/2009/11/parsing-multipart-form-data-in-wcf.html

    0 讨论(0)
  • 2020-12-30 16:31

    I open-sourced a C# Http form parser here.

    This is slightly more flexible than the other one mentioned which is on CodePlex, since you can use it for both Multipart and non-Multipart form-data, and also it gives you other form parameters formatted in a Dictionary object.

    This can be used as follows:

    non-multipart

    public void Login(Stream stream)
    {
        string username = null;
        string password = null;
    
        HttpContentParser parser = new HttpContentParser(data);
        if (parser.Success)
        {
            username = HttpUtility.UrlDecode(parser.Parameters["username"]);
            password = HttpUtility.UrlDecode(parser.Parameters["password"]);
        }
    }
    

    multipart

    public void Upload(Stream stream)
    {
        HttpMultipartParser parser = new HttpMultipartParser(data, "image");
    
        if (parser.Success)
        {
            string user = HttpUtility.UrlDecode(parser.Parameters["user"]);
            string title = HttpUtility.UrlDecode(parser.Parameters["title"]);
    
            // Save the file somewhere
            File.WriteAllBytes(FILE_PATH + title + FILE_EXT, parser.FileContents);
        }
    }
    
    0 讨论(0)
  • 2020-12-30 16:46

    I've had some issues with parser that are based on string parsing particularly with large files I found it would run out of memory and fail to parse binary data.

    To cope with these issues I've open sourced my own attempt at a C# multipart/form-data parser here

    See my answer here for more information.

    0 讨论(0)
提交回复
热议问题