0

When I upload a file using Amazon S3 multipart then it's loading, the file is not successfully uploaded. uploadRequest_UploadPartProgressEvent in this function facing loading problem after e.PercentDone is 100, web page is loading infinity time.

 protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                extension = Path.GetExtension(FileUpload1.FileName);
                fileSize = FileUpload1.PostedFile.ContentLength;

                    Stream st = FileUpload1.PostedFile.InputStream;
                    string name = Path.GetFileName(FileUpload1.FileName);
                    string fName = Path.GetFileNameWithoutExtension(name);
                    string fExt = Path.GetExtension(name);

                    string strRes = String.Concat(fName, builder.ToString());
                    string strRes1 = String.Concat(strRes, fExt);

                    string s3FileName = @strRes1;
                    stt = st;
                    fileNameInS3 = s3FileName;
                    bool a;
                    TrackMPUAsync().Wait();

            }
        }
         public async Task TrackMPUAsync()
        {
            try
            {
                TransferUtility fileTransferUtility = new TransferUtility(s3Client);
                // Use TransferUtilityUploadRequest to configure options.
                // In this example we subscribe to an event.
                TransferUtilityUploadRequest uploadRequest =
                    new TransferUtilityUploadRequest
                    {
                        BucketName = bucketName + @"/" + foldername,
                        Key = filename,
                        InputStream = fileInputStrem
                    };

                uploadRequest.UploadProgressEvent +=
                     new EventHandler<UploadProgressArgs>
                         (uploadRequest_UploadPartProgressEvent);


                await fileTransferUtility.UploadAsync(uploadRequest);
                Console.WriteLine("Upload completed");
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine("Error encountered on server. Message:'{0}' when writing an object", e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Unknown encountered on server. Message:'{0}' when writing an object", e.Message);
            }
        }

      public void uploadRequest_UploadPartProgressEvent(object sender, 
      UploadProgressArgs e)
        {
            // Process event.
            Console.WriteLine("{0}/{1}", e.TransferredBytes, e.TotalBytes);
        }

Has anyone else experienced this problem?

1 Answer 1

0

I have a couple suggestions for getting to a working solution, but to be honest I didn't run your code (this is based on reviewing it quickly). First, the BucketName property must be set to the exact name of the bucket[1], so you shouldn't be adding the folder name to it. Instead, the folder name should be added as the prefix on the Key:

BucketName = bucketName,
Key = foldername + @"/" + filename,

Next, using Console.WriteLine may may it difficult to see the errors caught. It appears this is part of an WinForms application, so perhaps using Debug.WriteLine()[2] to display the errors in Visual Studio (or Code, etc.) would be a good idea.

Finally, and perhaps importantly, the use of TrackMPUAsync().Wait() in the event handler is synchronous and will block until the upload completes[3], instead you may want to perform the upload asynchronously:

Task.Run(() => TrackMPUAsync());

Hope this is helpful.

[1] https://docs.aws.amazon.com/AmazonS3/latest/dev/HLuploadFileDotNet.html

[2] https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.debug.writeline

[3] Prevent winforms UI block when using async/await

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, when I using this 'Task.Run(() => TrackMPUAsync());' instated of TrackMPUAsync().Wait() this code then , TrackMPUAsync() this function is not executing. and how to update e.percentdone value to label. public void uploadRequest_UploadPartProgressEvent(object sender, UploadProgressArgs e) { Console.WriteLine("{0}/{1}", e.TransferredBytes, e.TotalBytes); Label1.Text=e.percentdone; } above code is not working while a file is uploading on aws s3. It shows value when e.percentdone is 100.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.