0

I am attempting to upload a file. The code below works on my local machine or running on a remote server running the dll from a command line, but when I try and publish to my test environment and run under iis it fails.

<form method="post" asp-action="Upload" asp-controller="Prebook" enctype="multipart/form-data">
    <div class="form-inline">
        <div class="col-md-2">
            <div class="form-group">
                <input type="file" name="files" data-input= "false" multiple class="filestyle"  data-buttonName="btn-primary">
            </div>
        </div>
        <div class="col-md-2">
            <div class="form-group">
                <input type="submit" value="Upload File" class="btn btn-primary" />
            </div>
        </div>
   </div>
</form>

Controller logic

[HttpPost]
public async Task<IActionResult> Upload(ICollection<IFormFile> files)
{
    if (await _contextPB.UploadRow.AnyAsync())

    {                
        Danger(string.Format("Please LOAD the existing containers before uploading another file"), true);
        return View();
    }
    int rowCount = 0;
    var uploads = Path.Combine(_environment.WebRootPath, "uploads");
    var _viewModel = new UploadViewModel();

    foreach (var file in files)
    {

        using (var streamReader = System.IO.File.OpenText(Path.Combine(uploads, file.FileName)))
        {
            var line = streamReader.ReadLine();
            var columnNames = line.Split(new[] { ',' });
            if (!ValidateColumnNames(columnNames))
            {
                Danger(string.Format("Invalid Column Name in Upload file"), true);
                return View(_viewModel);
            }
            while (!streamReader.EndOfStream)
            {
                var data = line.Split(new[] { ',' });
                var uploadRow = new UploadRow();                       
                // validation & assignment logic removed
                try
                {
                    _contextPB.Add(uploadRow);
                    rowCount++;
                }
                catch (Exception e)
                {
                    Danger(string.Format("<b>{0},{1}</b> database error", uploadRow.Container_Id, e), true);
                }
                line = streamReader.ReadLine();

            }
        }
    }
}

1 Answer 1

1

Try adding a catch block to see what the error is.

I'm assuming a permission issue.

[HttpPost]
public async Task<IActionResult> Upload(ICollection<IFormFile> files)
{
    try
    { 
        if (await _contextPB.UploadRow.AnyAsync())
        {
            Danger(string.Format("Please LOAD the existing containers before uploading another file"), true);
            return View();
        }

        // your code
    }
    catch (Exception ex)
    {
        // what is the error?
        // add a breakpoint to see
        throw;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.