简体   繁体   English

将文件从Html表单(multipart / form-data)上传到WCF REST服务作为流而不流式传输整个表单的输入?

[英]Upload file from Html form (multipart/form-data) to WCF REST service as a stream without streaming the whole form's inputs?

I'm currently having an issue while uploading a file from Html to my rest service (WCF REST). 我在将文件从Html上传到我的休息服务(WCF REST)时遇到了问题。 While uploading a file, I'd like to send information like Title and Description along with the file's Contents. 在上传文件时,我想发送标题和说明等信息以及文件的内容。

So, I've created a test form like this: 所以,我创建了一个这样的测试表单:

<form id="testForm" action="http://localhost.:1576/NotepadService.svc/Note/91f6413c-4d72-42ca-a0f3-38df15759fc9/Attachment" method="POST" enctype="multipart/form-data">
        <table>
            <tr><td>Title:</td><td><input type="text" name="Title"></td></tr>
            <tr><td>Description:</td><td><input type="text" name="Description"></td></tr>
            <tr><td>Filename:</td><td><input type="text" name="Filename"></td></tr>
            <tr><td>File:</td><td><input type="file" name="Contents"></td></tr>
            <tr><td/><td><input type="submit" value="Send"></td></tr>
        </table>
    </form>

Server-side, I'd like to translate it to this method: 服务器端,我想将其翻译为此方法:

[OperationContract]
        [WebInvoke(
            BodyStyle = WebMessageBodyStyle.Bare,
            Method = "POST",
            UriTemplate = "/Note/{noteId}/Attachment")]
        [Description("Add an attachment to a Note.")]
        void AddAttachmentToNote(string noteId, AttachmentRequestDto attachmentRequestDto);

With AttachmentRequestDto defined as 将AttachmentRequestDto定义为

[DataContract]
    public class AttachmentRequestDto
    {
         [DataMember]
         public string Title { get; set; }
         [DataMember]
         public string Description { get; set; }
         [DataMember]
         public string Filename { get; set; }
         [DataMember]
         public Stream Contents { get; set; }
    }

So, long story short, I'd like to get the Title and Description as string values, while getting the contents of the file as a stream. 所以,长话短说,我想将标题和描述作为字符串值,同时将文件的内容作为流获取。 This doesn't seem to work, as the html form will put all the contents of the form (so also Title and Description) into a stream, along with the contents of the file. 这似乎不起作用,因为html表单会将表单的所有内容(以及标题和描述)与文件的内容一起放入流中。 Therefor, defining my REST method as 因此,将我的REST方法定义为

[OperationContract]
        [WebInvoke(
            BodyStyle = WebMessageBodyStyle.Bare,
            Method = "POST",
            UriTemplate = "/Note/{noteId}/Attachment")]
        [Description("Add an attachment to a Note.")]
        void AddAttachmentToNote(string noteId, Stream formContents);

works, but then I need to parse the stream to get all of my data (which is not a nice approach, compared to what I actually want to do). 工作,但后来我需要解析流来获取我的所有数据(与我实际想做的相比,这不是一个好方法)。

Maybe I need to define 2 different service methods, one accepting only the file, and the other accepting the file's details? 也许我需要定义两种不同的服务方法,一种只接受文件,另一种接受文件的细节? That would, however, mean that my business rules (Title required + filecontents required) should be validated differently (as REST is stateless). 但是,这意味着我的业务规则(所需的标题+所需的文件内容)应该以不同的方式进行验证(因为REST是无状态的)。

Something that might be worth mentioning: I need to save the file's contents in the database, not on the file system. 值得一提的是:我需要将文件的内容保存在数据库中,而不是保存在文件系统中。

Does anyone have some input on this one? 有没有人对此有一些意见? I'm kind of stuck on it... 我有点卡在上面......

Thanks! 谢谢!

Please find some code that might help you to pass a file along with its details in a single call to the REST service: 请查找一些代码,这些代码可以帮助您在一次调用REST服务时传递文件及其详细信息:

First you would need something called a MultipartParser as shown below: 首先,您需要一个名为MultipartParser的东西,如下所示:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace SampleService
{
    public class MultipartParser
    {
        private byte[] requestData;
        public MultipartParser(Stream stream)
        {
            this.Parse(stream, Encoding.UTF8);
            ParseParameter(stream, Encoding.UTF8);
        }

        public MultipartParser(Stream stream, Encoding encoding)
        {
            this.Parse(stream, encoding);
        }

        private void Parse(Stream stream, Encoding encoding)
        {
            this.Success = false;

            // Read the stream into a byte array
            byte[] data = ToByteArray(stream);
            requestData = data;

            // Copy to a string for header parsing
            string content = encoding.GetString(data);

            // The first line should contain the delimiter
            int delimiterEndIndex = content.IndexOf("\r\n");

            if (delimiterEndIndex > -1)
            {
                string delimiter = content.Substring(0, content.IndexOf("\r\n"));

                // Look for Content-Type
                Regex re = new Regex(@"(?<=Content\-Type:)(.*?)(?=\r\n\r\n)");
                Match contentTypeMatch = re.Match(content);

                // Look for filename
                re = new Regex(@"(?<=filename\=\"")(.*?)(?=\"")");
                Match filenameMatch = re.Match(content);

                // Did we find the required values?
                if (contentTypeMatch.Success && filenameMatch.Success)
                {
                    // Set properties
                    this.ContentType = contentTypeMatch.Value.Trim();
                    this.Filename = filenameMatch.Value.Trim();

                    // Get the start & end indexes of the file contents
                    int startIndex = contentTypeMatch.Index + contentTypeMatch.Length + "\r\n\r\n".Length;

                    byte[] delimiterBytes = encoding.GetBytes("\r\n" + delimiter);
                    int endIndex = IndexOf(data, delimiterBytes, startIndex);

                    int contentLength = endIndex - startIndex;

                    // Extract the file contents from the byte array
                    byte[] fileData = new byte[contentLength];

                    Buffer.BlockCopy(data, startIndex, fileData, 0, contentLength);

                    this.FileContents = fileData;
                    this.Success = true;
                }
            }
        }

        private void ParseParameter(Stream stream, Encoding encoding)
        {
            this.Success = false;

            // Read the stream into a byte array
            byte[] data;
            if (requestData.Length == 0)
            {
                data = ToByteArray(stream);
            }
            else { data = requestData; }
            // Copy to a string for header parsing
            string content = encoding.GetString(data);

            // The first line should contain the delimiter
            int delimiterEndIndex = content.IndexOf("\r\n");

            if (delimiterEndIndex > -1)
            {
                string delimiter = content.Substring(0, content.IndexOf("\r\n"));
                string[] splitContents = content.Split(new[] {delimiter}, StringSplitOptions.RemoveEmptyEntries);
                foreach (string t in splitContents)
                {
                    // Look for Content-Type
                    Regex contentTypeRegex = new Regex(@"(?<=Content\-Type:)(.*?)(?=\r\n\r\n)");
                    Match contentTypeMatch = contentTypeRegex.Match(t);

                    // Look for name of parameter
                    Regex re = new Regex(@"(?<=name\=\"")(.*)");
                    Match name = re.Match(t);

                    // Look for filename
                    re = new Regex(@"(?<=filename\=\"")(.*?)(?=\"")");
                    Match filenameMatch = re.Match(t);

                    // Did we find the required values?
                    if (name.Success || filenameMatch.Success)
                    {
                        // Set properties
                        //this.ContentType = name.Value.Trim();
                        int startIndex;
                        if (filenameMatch.Success)
                        {
                            this.Filename = filenameMatch.Value.Trim();
                        }
                        if(contentTypeMatch.Success)
                        {
                            // Get the start & end indexes of the file contents
                            startIndex = contentTypeMatch.Index + contentTypeMatch.Length + "\r\n\r\n".Length;
                        }
                        else
                        {
                            startIndex = name.Index + name.Length + "\r\n\r\n".Length;
                        }

                        //byte[] delimiterBytes = encoding.GetBytes("\r\n" + delimiter);
                        //int endIndex = IndexOf(data, delimiterBytes, startIndex);

                        //int contentLength = t.Length - startIndex;
                        string propertyData = t.Substring(startIndex - 1, t.Length - startIndex);
                        // Extract the file contents from the byte array
                        //byte[] paramData = new byte[contentLength];

                        //Buffer.BlockCopy(data, startIndex, paramData, 0, contentLength);

                        MyContent myContent = new MyContent();
                        myContent.Data = encoding.GetBytes(propertyData);
                        myContent.StringData = propertyData;
                        myContent.PropertyName = name.Value.Trim();

                        if (MyContents == null)
                            MyContents = new List<MyContent>();

                        MyContents.Add(myContent);
                        this.Success = true;
                    }
                }
            }
        }

        private int IndexOf(byte[] searchWithin, byte[] serachFor, int startIndex)
        {
            int index = 0;
            int startPos = Array.IndexOf(searchWithin, serachFor[0], startIndex);

            if (startPos != -1)
            {
                while ((startPos + index) < searchWithin.Length)
                {
                    if (searchWithin[startPos + index] == serachFor[index])
                    {
                        index++;
                        if (index == serachFor.Length)
                        {
                            return startPos;
                        }
                    }
                    else
                    {
                        startPos = Array.IndexOf<byte>(searchWithin, serachFor[0], startPos + index);
                        if (startPos == -1)
                        {
                            return -1;
                        }
                        index = 0;
                    }
                }
            }

            return -1;
        }

        private byte[] ToByteArray(Stream stream)
        {
            byte[] buffer = new byte[32768];
            using (MemoryStream ms = new MemoryStream())
            {
                while (true)
                {
                    int read = stream.Read(buffer, 0, buffer.Length);
                    if (read <= 0)
                        return ms.ToArray();
                    ms.Write(buffer, 0, read);
                }
            }
        }

        public List<MyContent> MyContents { get; set; }

        public bool Success
        {
            get;
            private set;
        }

        public string ContentType
        {
            get;
            private set;
        }

        public string Filename
        {
            get;
            private set;
        }

        public byte[] FileContents
        {
            get;
            private set;
        }
    }

    public class MyContent
    {
        public byte[] Data { get; set; }
        public string PropertyName { get; set; }
        public string StringData { get; set; }
    }
}

Now define your REST method as shown: 现在定义您的REST方法,如下所示:

[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
AttachmentRequestDto AddAttachmentToNote(Stream stream);

Now the implemenation of the above method as shown: 现在实现上面的方法如图所示:

public AttachmentRequestDto AddAttachmentToNote(Stream stream)
        {
            MultipartParser parser = new MultipartParser(stream);
            if(parser != null && parser.Success)
            {
                foreach (var content in parser.MyContents)
                {
                    // Observe your string here which is a serialized version of your file or the object being passed. Based on the string do the necessary action.
                    string str = Encoding.UTF8.GetString(content.Data);

                }
            }

            return new AttachmentRequestDto();
        }

My AttachmentRequestDto looks as shown: My AttachmentRequestDto如下所示:

[DataContract]
    public class AttachmentRequestDto
    {
         [DataMember]
         public string Title { get; set; }
         [DataMember]
         public string Description { get; set; }
         [DataMember]
         public string Filename { get; set; }
    }

Now from the client i do perform a POST as shown : 现在从客户端我执行POST,如下所示:

Image image = Image.FromFile("C:\\Users\\Guest\\Desktop\\sample.png");
MemoryStream ms = new MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] imageArray = ms.ToArray();
ms.Close();

AttachmentRequestDto objAttachmentRequestDto = new AttachmentRequestDto();
objAttachmentRequestDto.Title = "Sample";
objAttachmentRequestDto.Description = "Sample book";
objAttachmentRequestDto.FileName = "SampleBook.png";

var serializer = new DataContractSerializer(typeof(AttachmentRequestDto));
var ms = new MemoryStream();
serializer.WriteObject(ms, objAttachmentRequestDto);
ms.Position = 0;
var reader = new StreamReader(ms);
string requestBody = reader.ReadToEnd();

var client = new RestClient();            
client.BaseUrl = "http://localhost/SampleService/Service1.svc";
var request = new RestRequest(method) { DateFormat = DataFormat.Xml.ToString(), Resource = resourceUrl };
if(requestBody !=null)
      request.AddParameter("objAttachmentRequestDto", requestBody);
request.AddFile("stream", image, "Array.png");
var response = client.Execute(request);

I do use a third party dll for the above code called RESTSharp . 我确实使用第三方dll作为名为RESTSharp的上述代码。

Once on the server the MultipartParser identifies your request boundary to split the necessary contents and then you can decide on what to do with each of the split contents (save to file,db, etc..) 一旦在服务器上,MultipartParser识别您的请求边界以分割必要的内容,然后您可以决定如何处理每个拆分内容(保存到文件,数据库等)。

Just make sure you have the approriate config entries for the above as well along with dataContractSerializer property and readerQuotas set for the webHttpBinding. 只需确保您具有上述的approriate配置条目以及为we​​bHttpBinding设置的dataContractSerializer属性和readerQuotas。

NOTE: The MultipartParser can be more re-factored if needed. 注意:如果需要,可以更多地重新考虑MultipartParser。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM