无法在ASP .NET中上传文件[关闭]

问题描述 投票:0回答:1

我正在使用带有Razor的ASP .NET并尝试上传文件。但它没有上传,也没有显示任何错误。

这是我的表格

<form method="post" enctype="multipart/form-data" action="/Account/ApplyJobTo">
<input name="__RequestVerificationToken" type="hidden" value="4dcsxIcQdtZTFIm80O-qZ4COPjvj_gm0EPCxUxVgVi8nKtIIl4rPHbw9IyNsHDsU-KUZCnBh6WrDyNiz-LQfMf-SxxOlaZw3Y3Ai4EZb11z44-J0c7XjAyqBSqm82OLJ0" />

<label for="job">Jobs</label>
<select name="job" id="job" required>
    <option disabled selected>Select Job</option>
<option value='1'>Manager</option><option value='2'>Directory</option>    </select>
<label for="cv">Upload CV (only PDF)</label>
<input type="file" required id="cv" name="File1" />
       <input type="submit" value="Apply" name="apply" />

</form>

这是我在ApplyJobTo中的代码

@{ 
var File1 = Request.Files;
foreach (string file in File1)
{
    var fileContent = Request.Files[file];
    if (fileContent != null && fileContent.ContentLength > 0)
    {
        if (fileContent.ContentType == "pdf")
        {
            var stream = fileContent.InputStream;
            // and optionally write the file to disk
            var fileName = (int)Session["user_id"] + "-" + fileContent.FileName;
            var path = Path.Combine(Server.MapPath("/Assets/CV/"), fileName.ToString());
            using (var fileStream = File.Create(path))
            {
                stream.CopyTo(fileStream);
                var _db = Database.Open("mcj");
                string job_id = Request.Form["job"];
                int user_id = (int)Session["user_id"];
                _db.Execute("INSERT INTO job_requests (job_id, user_id, cv) VALUES (@0, @1, @2)", job_id, user_id, fileName);

                Response.Write("Job Applied");
            }
        }
    }
    else
    {
        Response.Write("Only PDFs are allowed");
    }
}
}

此外,我在上传文件时写了一个查询,并且它没有添加到数据库中。我没有收到任何错误,只是一个空白页面。无法理解有什么不对。

c# asp.net razor
1个回答
1
投票

我在学习上传时遇到了类似的问题。问题是这一行

if (fileContent.ContentType == "pdf")

它应该是

if (fileContent.ContentType == "application/pdf")

这是MIME类型,以防您不知道。正确学习功能。祝好运 :)

© www.soinside.com 2019 - 2024. All rights reserved.