コード例 #1
0
 private async Task WriteDataToStreamAsync(Stream stream, MultipartField part)
 {
     if (part is MultipartFileField)
     {
         using (var inputStream = new FileStream(((MultipartFileField)part).FilePath, FileMode.Open, FileAccess.Read, FileShare.Read | FileShare.Delete))
         {
             await inputStream.CopyToAsync(stream);
         }
     }
     else if (part is MultipartTextField)
     {
         await WriteToStreamAsync(stream, Encoding.UTF8.GetBytes(((MultipartTextField)part).Value));
     }
 }
コード例 #2
0
        private async Task WritePartHeaderAsync(Stream stream, string name, MultipartField part)
        {
            var contentType = part.ContentType;

            await WriteToStreamAsync(stream, MULTIPART_HEADER_CONTENT_DISPOSITION_AND_NAME_BYTES);
            await WriteToStreamAsync(stream, Encoding.UTF8.GetBytes(Uri.EscapeDataString(name)));

            var isFile = part is MultipartFileField;

            if (isFile)
            {
                contentType = contentType ?? "application/octet-stream";

                var field = part as MultipartFileField;

                if (!string.IsNullOrEmpty(field.FileName))
                {
                    await WriteToStreamAsync(stream, MULTIPART_HEADER_END_NAME_AND_FILENAME);
                    await WriteToStreamAsync(stream, Encoding.UTF8.GetBytes(field.FileName.Replace("\"", "")));
                }
            }
            await WriteToStreamAsync(stream, MULTIPART_HEADER_END);
            await WriteToStreamAsync(stream, CRLF_BYTES);

            if (!string.IsNullOrEmpty(contentType))
            {
                await WriteToStreamAsync(stream, MULTIPART_HEADER_CONTENT_TYPE);
                await WriteToStreamAsync(stream, Encoding.UTF8.GetBytes(contentType));

                if (!isFile)
                {
                    await WriteToStreamAsync(stream, MULTIPART_HEADER_CONTENT_TYPE_END_CHARSET);
                    await WriteToStreamAsync(stream, Encoding.UTF8.GetBytes("utf-8"));
                }
                await WriteToStreamAsync(stream, CRLF_BYTES);
            }
        }