上传文件ASP核心IIS

时间:2016-11-22 10:05:32

标签: c# asp.net-core-mvc

我正在尝试上传文件。下面的代码可以在我的本地计算机上运行,​​也可以在从命令行运行dll的远程服务器上运行,但是当我尝试发布到我的测试环境并在iis下运行时,它会失败。

<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>

控制器逻辑

[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 个答案:

答案 0 :(得分:1)

尝试添加catch块以查看错误是什么。

我正在考虑许可问题。

[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;
    }
}