コード例 #1
0
        public static async Task UploadCopyToAsync(this Stream source, Stream destination, IProgress <UploadReport> progress, int bufferSize)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            int readByees  = 0;
            int sentBytes  = 0;
            int totalBytes = (int)source.Length;

            byte[] buffer = new byte[bufferSize];

            while ((readByees = await source.ReadAsync(buffer, 0, bufferSize).ConfigureAwait(false)) != 0)
            {
                await destination.WriteAsync(buffer, 0, readByees).ConfigureAwait(false);

                sentBytes += readByees;

                progress?.Report(UploadReport.CreateProcessing(sentBytes, totalBytes));
            }
        }
コード例 #2
0
        public static void UploadCopyTo(this Stream source, Stream destination, IProgress <UploadReport> progress, int bufferSize)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            int readByees  = 0;
            int sentBytes  = 0;
            int totalBytes = (int)source.Length;

            byte[] buffer = new byte[bufferSize];

            while ((readByees = source.Read(buffer, 0, bufferSize)) != 0)
            {
                destination.Write(buffer, 0, readByees);

                sentBytes += readByees;

                progress?.Report(UploadReport.CreateProcessing(sentBytes, totalBytes));
            }
        }
コード例 #3
0
        public async Task <string> ChunkedUploadAppend(long mediaId, Stream media, long sendSize, long actualSize, long totalLength, int index, IProgress <UploadReport> progressReceiver)
        {
            long dataLength = media.Length;

            progressReceiver?.Report(UploadReport.CreateBegin(dataLength));

            var result = await this.ChunkedUploadAppendImpl(mediaId, media, sendSize, actualSize, totalLength, index, progressReceiver).ConfigureAwait(false);

            progressReceiver?.Report(UploadReport.CreateCompleted(dataLength));

            return(result);
        }
コード例 #4
0
        public ActionResult UploadReport(int id)
        {
            UploadReport report = new UploadReport();

            using (DBClass context = new DBClass())
            {
                DataTable data = context.getData("select ud.FileName ,us.uCompanyName from UploadData ud left join UserDetail us on ud.UserName=us.UserName where ud.ID = '" + id + "'", CommandType.Text);
                report.FileName = Convert.ToString(data.Rows[0]["FileName"]);
                report.Company  = Convert.ToString(data.Rows[0]["uCompanyName"]);
                ViewBag.Message = "Upload Report";
            }
            return(PartialView("_UploadReport", report));
        }
コード例 #5
0
        public async Task <MediaResponse> Upload(Stream contentStream, long[] additionalOwners = null, IProgress <UploadReport> progressReceiver = null)
        {
            long contentLength = contentStream.Length;

            progressReceiver?.Report(UploadReport.CreateBegin(contentLength));

            var bondary = OAuthHelper.GenerateNonce();

            using var content = new MultipartFormDataContent(bondary);

            if (additionalOwners?.Length > 0)
            {
                var joinedAdditionalOwners = string.Join(",", additionalOwners);
                content.Add(new StringContent(joinedAdditionalOwners), "additional_owners");
            }

            content.Add(new ProgressableUploadContent(contentStream, progressReceiver: progressReceiver), "media");

            var result = await this.Api.ApiPostRequestAsync <MediaResponse>(_apiEndpoint, content);

            progressReceiver?.Report(UploadReport.CreateCompleted(contentLength));

            return(result);
        }
コード例 #6
0
        public ActionResult UploadReport(int id, FormCollection formcollection)
        {
            bool sts = ModelState.IsValid;

            if (ModelState.IsValid)
            {
                var file = HttpContext.Request.Files["UploadedReport"];
                if (file == null)
                {
                    ModelState.AddModelError("File", "Please Upload Your file");
                }
                else if (file.ContentLength > 0)
                {
                    int      MaxContentLength      = 1024 * 1024 * 100; //100 MB
                    string[] AllowedFileExtensions = new string[] { ".pdf", ".docx", ".txt", ".jpg", ".png", ".doc" };
                    string   ext = file.FileName.Substring(file.FileName.LastIndexOf('.'));
                    if (!AllowedFileExtensions.Contains(file.FileName.Substring(file.FileName.LastIndexOf('.'))))
                    {
                        ModelState.AddModelError("File", "Please file of type: " + string.Join(", ", AllowedFileExtensions));
                    }
                    else if (file.ContentLength > MaxContentLength)
                    {
                        ModelState.AddModelError("File", "Your file is too large, maximum allowed size is: " + MaxContentLength + " MB. Please zip your file and upload agail.");
                    }
                    else
                    {
                        var    fileName = Path.GetFileName(file.FileName);
                        string fname    = id + "@" + fileName;
                        var    path     = Path.Combine(Server.MapPath("~/Reports"), fname);
                        using (DBClass context = new DBClass())
                        {
                            context.AddParameter("@ID", id);
                            context.AddParameter("@EmpID", Session["UserName"].ToString());
                            context.AddParameter("@ReportFile", fileName);
                            if (context.ExecuteNonQuery("addReport", CommandType.StoredProcedure) > 0)
                            {
                                if (System.IO.File.Exists(path))
                                {
                                    System.IO.File.Delete(path);
                                    file.SaveAs(path);
                                    ModelState.Clear();
                                    ViewBag.Message = "Report uploaded successfully";
                                    return(RedirectToAction("ClientList", "Clients"));
                                    //ModelState.AddModelError("File", "This file is already exist!");
                                }
                                else
                                {
                                    file.SaveAs(path);
                                    ModelState.Clear();
                                    ViewBag.Message = "Report uploaded successfully";
                                    return(RedirectToAction("ClientList", "Clients"));
                                }
                            }
                            else
                            {
                                ModelState.AddModelError("", "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.");
                            }
                        }
                    }
                }
            }
            UploadReport report = new UploadReport();

            using (DBClass context = new DBClass())
            {
                DataTable data = context.getData("select ud.FileName ,us.uCompanyName from UploadData ud left join UserDetail us on ud.UserName=us.UserName where ud.ID = '" + id + "'", CommandType.Text);
                report.FileName = Convert.ToString(data.Rows[0]["FileName"]);
                report.Company  = Convert.ToString(data.Rows[0]["uCompanyName"]);
                ViewBag.Message = "Upload Report";
            }
            return(PartialView("_UploadReport", report));
        }
コード例 #7
0
        /// <param name="uploadData"></param>
        /// <param name="mediaType"></param>
        /// <param name="additionalOwners"></param>
        /// <param name="progressReceiver"></param>
        /// <param name="segmentSize"></param>
        /// <returns></returns>
        public async Task <MediaResponse> ChunkedUpload(Stream uploadData, string mediaType, long[] additionalOwners = null,
                                                        IProgress <UploadReport> progressReceiver = null, int segmentSize = 1024 * 1024 * 1)
        {
            if (segmentSize <= 0)
            {
                throw new ArgumentException($"{nameof(segmentSize)}には0より大きい値を指定してください。");
            }

            if (uploadData.CanSeek)
            {
                uploadData.Position = 0;
            }

            long fileLength = uploadData.Length;

            if (fileLength <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(uploadData));
            }

            // INITコマンド
            var initResposne = await this.ChunkedUploadInit(fileLength, mediaType, additionalOwners)
                               .ConfigureAwait(false);

            long mediaId = initResposne.MediaId;

            // APPENDコマンド
            // アップロード開始
            progressReceiver?.Report(UploadReport.CreateBegin(fileLength));

            long chunkSize  = segmentSize;
            long actualSize = 0;
            int  index      = 0;

            while (actualSize < fileLength)
            {
                if (fileLength < (actualSize + chunkSize))
                {
                    chunkSize = fileLength - actualSize;
                }

                var res = await this.ChunkedUploadAppendImpl(mediaId, uploadData, chunkSize, actualSize, fileLength, index, progressReceiver)
                          .ConfigureAwait(false);

                if (!string.IsNullOrEmpty(res))
                {
                    throw new TwitterException(res);
                }

                actualSize += chunkSize;
                ++index;
            }

            // FINALIZEコマンド
            var response = await this.ChunkedUploadFinalize(mediaId)
                           .ConfigureAwait(false);

            progressReceiver?.Report(UploadReport.CreateCompleted(fileLength));

            return(response);
        }