locked
File upload error message from the hosting server RRS feed

  • Question

  • User-164452226 posted

    Greetings

    l am getting an error message when l try to load a file on the hosting server. On the local machine where l am doing the coding, everything is working fine. Here is the error message and the code:

    Section 1 - error message

    Server Error in '/elearn' Application.
    Object reference not set to an instance of an object.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    
    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
    
    Source Error:
    
    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
    
    Stack Trace:
    
    
    [NullReferenceException: Object reference not set to an instance of an object.]
       elearning.Controllers.TeacherMenuController.InsertTeacherLessonUpload(TeacherLessonUpload model) in C:\Users\TPI\Source\Repos\elearning\elearning\Controllers\TeacherMenuController.cs:431

    Section 2 - File upload Code

           [HttpPost]
            public ActionResult InsertTeacherLessonUpload(TeacherLessonUpload model)
            {
                //Use Namespace called :  System.IO  
                string NotesName = Path.GetFileNameWithoutExtension(model.NotesImageFile.FileName);
                string VideoName = Path.GetFileNameWithoutExtension(model.VideoImageFile.FileName);
    
                //To Get File Extension  
                string NotesExtension = Path.GetExtension(model.NotesImageFile.FileName);
                string VideoExtension = Path.GetExtension(model.VideoImageFile.FileName);
    
                //Add Current Date To Attached File Name  
                NotesName = DateTime.Now.ToString("yyyyMMdd") + "-" + NotesName.Trim() + NotesExtension;
                VideoName = DateTime.Now.ToString("yyyyMMdd") + "-" + VideoName.Trim() + VideoExtension;
    
                //Get Upload path from Web.Config file AppSettings.  
                string UploadNotesPath = ConfigurationManager.AppSettings["NotesImagePath"].ToString();
                string UploadVideoPath = ConfigurationManager.AppSettings["VideoImagePath"].ToString();
    
                //Its Create complete path to store in server.  
                model.NotesPath = UploadNotesPath + NotesName;
                model.VideoPath = UploadVideoPath + VideoName;
    
                //To copy and save file into server.  
                model.NotesImageFile.SaveAs(model.NotesPath);
                model.VideoImageFile.SaveAs(model.VideoPath);
    
                if (ModelState.IsValid)
                {
                    object[] parameters = { model.UploadID, model.NotesPath, model.VideoPath, model.LessonID, model.DeliveryID, model.StaffID };
                    objTeacherLessonUpload.Insert(parameters);
                }
                return RedirectToAction("TeacherLessonUploadlndex");
            }

    Section 3- Web.Config

      <appSettings>
        <add key="webpages:Version" value="3.0.0.0"/>
        <add key="webpages:Enabled" value="false"/>
        <add key="ClientValidationEnabled" value="true"/>
        <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
        <add key="ReportPath" value="/Reports/"/>
         <add key="NotesImagePath" value="C:\NotesImagePath\"/>
        <add key="VideoImagePath" value="C:\VideoImagePath\"/>
        <add key="owin:AutomaticAppStartup" value="Startup1"/>
      </appSettings>

    The path below, which is also in the error msg, is from my local laptop but l don't why the hosting server is picking it

    sertTeacherLessonUpload(TeacherLessonUpload model) in C:\Users\TPI\Source\Repos\elearning\elearning\Controllers\TeacherMenuController.cs:431

    Tuesday, October 27, 2020 7:45 AM

All replies

  • User1686398519 posted

    Hi tmatiwure, 

    C:\NotesImagePath\

    According to the code you provided, the file is stored in an absolute path, so you can find the path on your local machine.

    You can use a virtual path, and then use the Server.MapPath method to convert the virtual path into a physical path that the server can understand.

    string UploadNotesPath =Server.MapPath(ConfigurationManager.AppSettings["NotesImagePath"].ToString());
    if (!Directory.Exists(UploadNotesPath))
    {
        Directory.CreateDirectory(UploadNotesPath);
    }
    model.NotesPath= UploadNotesPath + NotesName;

    Web.config

    <appSettings>		
    <add key="NotesImagePath" value="~\NotesImagePath"/>
    <add key="VideoImagePath" value="~\VideoImagePath"/> </appSettings>

    Best Regards,

    YihuiSun

    Wednesday, October 28, 2020 6:47 AM
  • User-164452226 posted

    Thank you so much for your effort @YihuiSu, l am still getting the same error.

    Thursday, October 29, 2020 10:34 AM
  • User753101303 posted

    Hi,

    Generally speaking it means you are trying to access an object member while the object itself is null. There is nothing showing how objTeacherLessonUpload is declared and created but this is just a guess.

    If you can't find you can add checks such as :

    if(objTeacherLessonUpload==null) throw new ArgumentNullException(nameof(objTeacherLessonUpload));

    to narrow down what is null exactly.

    Thursday, October 29, 2020 12:12 PM
  • User-164452226 posted

    l have altered:

                string UploadNotesPath = ConfigurationManager.AppSettings["NotesImagePath"].ToString();
                string UploadVideoPath = ConfigurationManager.AppSettings["VideoImagePath"].ToString();

    To:

                var UploadNotesPath = Path.Combine(Server.MapPath("~/NotesImagePath"));
                var UploadVideoPath = Path.Combine(Server.MapPath("~/VideoImagePath"));

    And its now working but the challenge now is that l seem not to be able to increase file upload size, l want video sizes to be 200 MB maximum. l have tried:

    %windir%\system32\inetsrv\appcmd set config -section:requestFiltering -requestLimits.maxAllowedContentLength:30000000

    error is:

    HTTP Error 404.13 - Not Found
    The request filtering module is configured to deny a request that exceeds the request content length.
    

    When l try to alter that, the error comes back on a yellow page but it still points to "limits exceeded" issue.

    Please help, the app is hosted on Windows 2012 Server R2

    Thursday, October 29, 2020 2:43 PM
  • User-474980206 posted

    the error from the IIS request filtering module. you must reconfigure it on the server:

      https://docs.microsoft.com/en-us/iis/manage/configuring-security/configure-request-filtering-in-iis

    note: asp.net also has a max request size setting

    Thursday, October 29, 2020 9:51 PM