Пример #1
0
        private static bool PreparationUpload(HttpWebRequest request, IEnumerable <HttpPostValue> blobs)
        {
            if (request == null)
            {
                return(false);
            }
            string tokenid  = Guid.NewGuid().ToString(); // "D"
            string boundary = "---------------" + (ulong)Hash64.GetHashCode(tokenid);

            request.Method      = "POST";
            request.ContentType = "multipart/form-data; boundary=" + boundary;
            byte[]       buffer = null;
            MemoryStream ms     = null;

            try
            {
                if (blobs != null)
                {
                    ms     = new MemoryStream();
                    buffer = new byte[570];
                    foreach (HttpPostValue blob in blobs)
                    {
                        if (blob == null)
                        {
                            throw new ArgumentNullException("blob");
                        }
                        if (blob.IsText)
                        {
                            byte[] cch = Encoding.UTF8.GetBytes(string.Format(
                                                                    "\r\n--{0}" +
                                                                    "\r\nContent-Disposition: form-data; name=\"{1}\"" +
                                                                    "\r\n\r\n{2}\r\n", boundary, blob.Key, blob.Value));
                            ms.Write(cch, 0, cch.Length);
                        }
                        else
                        {
                            byte[] cch = Encoding.UTF8.GetBytes(string.Format(
                                                                    "\r\n--{0}\r\n" +
                                                                    "Content-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\n" +
                                                                    "Content-Type: application/octet-stream\r\n\r\n", boundary, blob.Key, blob.FileName));
                            ms.Write(cch, 0, cch.Length);

                            cch = blob.Value;
                            if (cch != null)
                            {
                                ms.Write(cch, 0, cch.Length);
                            }
                            else
                            {
                                using (FileStream fs = new FileStream(blob.FileName, FileMode.Open, FileAccess.Read))
                                {
                                    try
                                    {
                                        int len = 0;
                                        while ((len = fs.Read(buffer, 0, buffer.Length)) != 0)
                                        {
                                            ms.Write(buffer, 0, len);
                                        }
                                    }
                                    catch (Exception)
                                    {
                                        return(false);
                                    }
                                }
                            }
                        }
                    }
                }
                if (blobs != null)
                {
                    byte[] cch = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");
                    ms.Write(cch, 0, cch.Length);
                }
                request.ContentLength = ms.Length;
                if (blobs != null)
                {
                    using (Stream rs = request.GetRequestStream())
                    {
                        ms.Position = 0;
                        int len = 0;
                        while ((len = ms.Read(buffer, 0, buffer.Length)) != 0)
                        {
                            rs.Write(buffer, 0, len);
                        }
                    }
                }
                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
            finally
            {
                if (ms != null)
                {
                    ms.Dispose();
                }
            }
        }