locked
Fileupload works for local machine but doesn't work for server RRS feed

  • Question

  • User1052662409 posted

    Hi All,

    My code working for uploading and downloading files for my local machine. When I run the application through Visual Studio it uploads the file with out any issue.

    But when I host my application it doesn't upload file. Below is my code, I guess there is something path issue (although I am not sure but guessing). 

    Because before I upload file on server, first I am uploading it to the root directory's folder.

               string supporting_docs = string.Empty;
                string filename = FileUpload1.FileName;
    
                if (File.Exists(Server.MapPath("DPRExpenseDocs/" + filename)))
                {
                    WebMsgBox.Show("File already exists");
    
                }
                else
                {
                    if (FileUpload1.HasFile)
                    {
    
                        FileUpload1.SaveAs(Server.MapPath("DPRExpenseDocs/" + filename));
                        supporting_docs = "DPRExpenseDocs/" + filename;
                        string bucketname = "XXXXXXXXXXXXX";
                        string path = Server.MapPath("DPRExpenseDocs/" + filename);
                        UploadFiles obj = new UploadFiles();
                        bool uploaded;
                        uploaded = obj.sendMyFileToS3(bucketname, filename, path);
                        if (uploaded)
                        {
                            WebMsgBox.Show("uploaded");
                        }
                        else
                        {
                            WebMsgBox.Show("Server error");
                        }
    
                    }

    Please suggest.

    Tuesday, June 25, 2019 5:27 PM

Answers

  • User475983607 posted

    It uploads file only in root's DPRExpenseDocs, but doesn't show any message. But dont know why? But on local machine it shows message.

    So the issue us with a 3rd party API named WebMsgBox?  Have you tried basic troubleshooting like opening Dev Tools (F12) and looking for errors in the console?  

    I recommend using standard ASP.NET error handling.  The code is completely dependent on WebMsgBox.  IMHO, removing the try...catches would be a better troubleshooting tool at this point.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 25, 2019 6:21 PM

All replies

  • User753101303 posted

    Hi,

    demoninside9

    it doesn't upload file

    Instead tell what happens. Assuming the page is just shown again as expected but without showing any client side message (I assume this is what WebMsgBox does ?) it seems it could happen if FileUpload1.HasFile is false : add also an else clause that shows something.

    If you are supposed to see a message in all cases, could it be that WebMsgBox doesn't work for now (maybe using scripts that are not loaded property ?) which prevents for now to understand what happens exactly ??

    Edit: or if it fails you should find the error message in the Windows event log. It's easier to start from the actual error than reading the code and trying to guess (for example even with this snippet you can already imagine maybe 3 or 4 issues and then you still have to check them).

    Tuesday, June 25, 2019 5:47 PM
  • User1052662409 posted

    PatriceSc

    If you are supposed to see a message in all cases, could it be that WebMsgBox doesn't work for now (maybe using scripts that are not loaded property ?) which prevents for now to understand what happens exactly ??

    It uploads file only in root's DPRExpenseDocs, but doesn't show any message. But dont know why? But on local machine it shows message.

    Below is my Uploadsfiles class

     public bool sendMyFileToS3(string bucketName, string keyName, string filePath)
        {
    
            try
            {
                var client = new AmazonS3Client(RegionEndpoint.APSouth1);
                PutObjectRequest putRequest = new PutObjectRequest
                {
                    BucketName = bucketName,
                    Key = keyName,
                    FilePath = filePath,
                    ContentType = "text/plain"
                };
                PutObjectResponse response = client.PutObject(putRequest);
            }
            catch (AmazonS3Exception amazonS3Exception)
            {
                if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity")))
                {
                    WebMsgBox.Show("Check the provided AWS Credentials.");
                }
                else
                {
                    WebMsgBox.Show("Error occurred: " + amazonS3Exception.Message);
                }
            }
    
    
            return true; //indicate that the file was sent  
        }
    Tuesday, June 25, 2019 5:54 PM
  • User753101303 posted

    So it seems WebMsgBox.Show doesn't work ? It won't work if it shows a message on the server side (this is a common catch with MessageBox.Show).

    If it is client side, try maybe F12 Network and make sure all the scripts it needs are properly loaded. It it shows a fake dialog the user must approve it could be better to have a reserved area the user can read but doesn't have to acknowledge each time using a button before proceeding further...

    Edit: this is taken from https://www.c-sharpcorner.com/article/Asp-Net-messagebox/ ?

    Tuesday, June 25, 2019 6:05 PM
  • User1052662409 posted

    So it seems WebMsgBox.Show doesn't work ?

    But I am hosting the same files which I am using on my local machine. ow it can be possible. I published first then upload it on server.

    Tuesday, June 25, 2019 6:09 PM
  • User1052662409 posted

    Yes

    Tuesday, June 25, 2019 6:19 PM
  • User475983607 posted

    It uploads file only in root's DPRExpenseDocs, but doesn't show any message. But dont know why? But on local machine it shows message.

    So the issue us with a 3rd party API named WebMsgBox?  Have you tried basic troubleshooting like opening Dev Tools (F12) and looking for errors in the console?  

    I recommend using standard ASP.NET error handling.  The code is completely dependent on WebMsgBox.  IMHO, removing the try...catches would be a better troubleshooting tool at this point.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 25, 2019 6:21 PM