コード例 #1
0
        public static string upload(string uri, string filePath, object obj, string fileKeyName = "file", int timeOut = 360000)
        {
            string result = "";

            WebRequest request = WebRequest.Create(uri);

            request.Timeout = timeOut;
            FormDataUtilsEx form = new FormDataUtilsEx();

            form.AddFormField(obj);
            FileStream file = new FileStream(filePath, FileMode.Open);

            byte[] bb = new byte[file.Length];
            file.Read(bb, 0, (int)file.Length);
            file.Close();
            string fileName = Path.GetFileName(filePath);

            form.AddStreamFile(fileKeyName, fileName, bb);
            form.PrepareFormData();
            request.ContentType = "multipart/form-data; boundary=" + form.Boundary;
            request.Method      = "Post";
            Stream stream = request.GetRequestStream();

            foreach (var b in form.GetFormData())
            {
                stream.WriteByte(b);
            }
            stream.Close();
            WebResponse response = request.GetResponse();

            using (var httpStreamReader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")))
            {
                result = httpStreamReader.ReadToEnd();
            }
            response.Close();
            request.Abort();
            return(result);
        }
コード例 #2
0
        public static string uploadFat(string uri, string filePath, object obj,
                                       string fileKeyName = "file", int timeOut = 3600000)
        {
            string          result  = "";
            FormDataUtilsEx form    = new FormDataUtilsEx();
            HttpWebRequest  request = (HttpWebRequest)WebRequest.Create(uri);

            request.ContentType = "multipart/form-data; boundary=" + form.Boundary;
            request.Method      = "Post";
            //request.AllowWriteStreamBuffering = false;
            request.Timeout = timeOut;

            //form data
            form.AddFormField(obj);
            string fileName = Path.GetFileName(filePath);

            form.AddStreamFile(fileKeyName, fileName);

            FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read);
            //二进制对象
            BinaryReader r = new BinaryReader(file);

            try
            {
                Stream stream = request.GetRequestStream();
                foreach (var b in form.GetFormData())
                {
                    stream.WriteByte(b);
                }
                //每次上传4k
                int    bufferLength = 4096;
                byte[] buffer       = new byte[bufferLength]; //已上传的字节数
                long   offset       = 0;                      //开始上传时间
                int    size         = r.Read(buffer, 0, bufferLength);
                long   length       = file.Length;
                while (size > 0)
                {
                    stream.Write(buffer, 0, size);
                    offset += size;
                    Application.DoEvents();
                    size = r.Read(buffer, 0, bufferLength);
                }

                foreach (var b in form.getSuffix())
                {
                    stream.WriteByte(b);
                }
                stream.Close();

                WebResponse response = request.GetResponse();

                using (var httpStreamReader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")))
                {
                    result = httpStreamReader.ReadToEnd();
                }
                response.Close();
                request.Abort();
                return(result);
            }
            catch (Exception ex)
            {
                SysLogUtils.error4Sys(LOG_UPLOADFAT, ex.Message);
                return("");
            }
            finally
            {
                file.Close();
                r.Close();
            }
        }