Ejemplo n.º 1
0
        public string UploadByUrl(string attachName, string url)
        {
            ResponseMessage result = new ResponseMessage();

            try
            {
                using (WebClient client = new WebClient())
                {
                    using (Stream reader = client.OpenRead(url))
                    {
                        result = Uploader.CreateUploader(attachName, (int)reader.Length);
                        if (result.errorFlag == "00")
                        {
                            int    bufferLength       = 1024 * 1024 * 3;
                            byte[] buffer             = new byte[bufferLength];
                            int    sortNo             = 0;
                            int    unSendBufferLength = (int)reader.Length;
                            while (unSendBufferLength > 0)
                            {
                                if (unSendBufferLength < bufferLength)
                                {
                                    bufferLength = unSendBufferLength;
                                    buffer       = new byte[bufferLength];
                                }
                                int size = reader.Read(buffer, 0, buffer.Length);

                                var upResult = result.uploader.Upload(buffer, ++sortNo);
                                if (upResult.errorFlag != "00")
                                {
                                    throw new Exception(upResult.errorMsg);
                                }
                                unSendBufferLength = unSendBufferLength - bufferLength;
                            }
                            result = result.uploader.FinishUpload();
                            if (result.errorFlag != "00")
                            {
                                throw new Exception(result.errorMsg);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result.errorFlag = "01";
                result.errorMsg  = ex.Message;
                Logger.SaveLog(ex.ToString());
            }
            return(result.ToString());
        }
Ejemplo n.º 2
0
        public string UploadByByteArray(string attachName, byte[] content)
        {
            ResponseMessage result = new ResponseMessage();

            try
            {
                result = Uploader.CreateUploader(attachName, content.Length);
                if (result.errorFlag == "00")
                {
                    using (MemoryStream stream = new MemoryStream(content))
                    {
                        int    bufferLength       = 1024 * 1024 * 3;
                        byte[] buffer             = new byte[bufferLength];
                        int    unSendBufferLength = (int)stream.Length;
                        int    sortNo             = 0;
                        while (unSendBufferLength > 0)
                        {
                            if (unSendBufferLength < bufferLength)
                            {
                                bufferLength = unSendBufferLength;
                                buffer       = new byte[bufferLength];
                            }
                            stream.Read(buffer, 0, buffer.Length);
                            var upResult = result.uploader.Upload(buffer, ++sortNo);
                            if (upResult.errorFlag != "00")
                            {
                                throw new Exception(upResult.errorMsg);
                            }
                            unSendBufferLength = unSendBufferLength - bufferLength;
                        }
                        result = result.uploader.FinishUpload();
                        if (result.errorFlag != "00")
                        {
                            throw new Exception(result.errorMsg);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                result.errorFlag = "01";
                result.errorMsg  = ex.Message;
                Logger.SaveLog(ex.ToString());
            }
            return(result.ToString());
        }
Ejemplo n.º 3
0
        public string BeginSegmentUpload(string attachName, int contentSize)
        {
            ResponseMessage result = new ResponseMessage();

            try
            {
                //通过授信码获取应用信息
                result = Uploader.CreateUploader(attachName, contentSize);
            }
            catch (Exception ex)
            {
                result.errorFlag = "01";
                result.errorMsg  = ex.Message;
                result.attachId  = string.Empty;
                Logger.SaveLog(ex.ToString());
            }
            return(result.ToString());
        }