Example #1
0
        public string Upload(IUriBuilder uriBuilder, Dictionary <string, object> parameters, params File[] files)
        {
            var uri       = uriBuilder.Build();
            var mimeParts = new List <MimePart>();

            try
            {
                var request = WebRequest.Create(uri);

                foreach (var key in parameters.Keys)
                {
                    var part = new StringMimePart();

                    part.Headers["Content-Disposition"] = "form-data; name=\"" + key + "\"";
                    part.StringData = parameters[key].ToString();

                    mimeParts.Add(part);
                }

                var nameIndex = 0;

                foreach (var file in files)
                {
                    var part = new StreamMimePart();

                    if (string.IsNullOrEmpty(file.FieldName))
                    {
                        file.FieldName = "file" + nameIndex++;
                    }

                    part.Headers["Content-Disposition"] = "form-data; name=\"" + file.FieldName + "\"; filename=\"" + file.Path + "\"";
                    part.Headers["Content-Type"]        = File.ContentType;

                    part.SetStream(file.Data);

                    mimeParts.Add(part);
                }

                var boundary = "----------" + DateTime.Now.Ticks.ToString("x");

                request.ContentType = "multipart/form-data; boundary=" + boundary;
                request.Method      = "POST";

                var footer = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");

                var contentLength = mimeParts.Sum(part => part.GenerateHeaderFooterData(boundary));

                request.ContentLength = contentLength + footer.Length;

                var buffer    = new byte[8192];
                var afterFile = Encoding.UTF8.GetBytes("\r\n");

                using (var s = request.GetRequestStream())
                {
                    foreach (var part in mimeParts)
                    {
                        s.Write(part.Header, 0, part.Header.Length);

                        int read;
                        while ((read = part.Data.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            s.Write(buffer, 0, read);
                        }

                        part.Data.Dispose();

                        s.Write(afterFile, 0, afterFile.Length);
                    }

                    s.Write(footer, 0, footer.Length);
                }

                return(GetContent(request, (statusCode, content) =>
                                  string.Format("Upload failed. Parameters: uri = {0}. Files: {1}. Response: {2} - {3}",
                                                uri.AbsoluteUri,
                                                string.Join(";", files.Select(x => x.Path)),
                                                statusCode,
                                                content)));
            }
            finally
            {
                foreach (var mimePart in mimeParts.Where(part => part.Data != null))
                {
                    mimePart.Data.Dispose();
                }
            }
        }
            public static HttpWebResponse Upload(HttpWebRequest req, UploadFile[] files, NameValueCollection form)
            {
                List<MimePart> mimeParts = new List<MimePart>();

                try
                {
                    foreach (string key in form.AllKeys)
                    {
                        StringMimePart part = new StringMimePart();

                        part.Headers["Content-Disposition"] = "form-data; name=\"" + key + "\"";
                        part.StringData = form[key];

                        mimeParts.Add(part);
                    }

                    int nameIndex = 0;

                    foreach (UploadFile file in files)
                    {
                        StreamMimePart part = new StreamMimePart();

                        if (string.IsNullOrEmpty(file.FieldName))
                            file.FieldName = "file" + nameIndex++;

                        part.Headers["Content-Disposition"] = "form-data; name=\"" + file.FieldName + "\"; filename=\"" + file.FileName + "\"";
                        part.Headers["Content-Type"] = file.ContentType;

                        part.SetStream(file.Data);

                        mimeParts.Add(part);
                    }

                    string boundary = "----------" + DateTime.Now.Ticks.ToString("x");

                    req.ContentType = "multipart/form-data; boundary=" + boundary;
                    req.Method = "POST";

                    long contentLength = 0;

                    byte[] _footer = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");

                    foreach (MimePart part in mimeParts)
                    {
                        contentLength += part.GenerateHeaderFooterData(boundary);
                    }

                    req.ContentLength = contentLength + _footer.Length;

                    byte[] buffer = new byte[8192];
                    byte[] afterFile = Encoding.UTF8.GetBytes("\r\n");
                    int read;

                    using (Stream s = req.GetRequestStream())
                    {
                        foreach (MimePart part in mimeParts)
                        {
                            s.Write(part.Header, 0, part.Header.Length);

                            while ((read = part.Data.Read(buffer, 0, buffer.Length)) > 0)
                                s.Write(buffer, 0, read);

                            part.Data.Dispose();

                            s.Write(afterFile, 0, afterFile.Length);
                        }

                        s.Write(_footer, 0, _footer.Length);
                    }

                    return (HttpWebResponse)req.GetResponse();
                }
                catch
                {
                    foreach (MimePart part in mimeParts)
                        if (part.Data != null)
                            part.Data.Dispose();

                    throw;
                }
            }