在 asp.net 应用程序网络表单中上传文件时找不到文件错误

Could not find file error on uploading files in asp.net application webforms

我想使用asp.net application webforms上传图片等文件。
申请 .netfaramwork4.6.1.
我对网站项目没有任何问题,但网络表单会抛出异常错误。
这是我的 aspx 页面代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="uploadfile.aspx.cs" Inherits="fileUploadTest.uploadfile" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>upload image</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <p>Browse to Upload File</p>
            <asp:FileUpload ID="FileUpload1" runat="server" />
        </div>
        <p>
            <asp:Button ID="Button1" runat="server" Text="Upload File" OnClick="Button1_Click" />
        </p>
    </form>
    <p>
        <asp:Label runat="server" ID="FileUploadStatus"></asp:Label>
    </p>
</body>
</html>

aspx.cs 后面的代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace fileUploadTest
{
    public partial class uploadfile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if ((FileUpload1.PostedFile != null) && (FileUpload1.PostedFile.ContentLength > 0))
            {
                string fn = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
                string SaveLocation = Server.MapPath("upload") + "\" + fn;
                try
                {
                    FileUpload1.PostedFile.SaveAs(SaveLocation);
                    FileUploadStatus.Text = "The file has been uploaded.";
                }
                catch (Exception ex)
                {
                    FileUploadStatus.Text = "Error: " + ex.Message;
                }
            }
            else
            {
                FileUploadStatus.Text = "Please select a file to upload.";
            }
        }
    }
}

我已经在 application 根目录中创建了一个文件夹,并将其命名为 upload

但是当我从我的系统中选择文件后点击按钮时,它会抛出如下错误:

Error: Could not find file 'C:\Users\mybla\Documents\Visual Studio 2017\Projects. Learning\fileUploadTest\fileUploadTest\upload\logo.png'.

logo.png 是我从系统中选择的文件。我什至试试这个代码:

if (Directory.Exists(Server.MapPath("~/upload/"))){
      FileUpload1.SaveAs(Server.MapPath("~/upload/" + fileName));
}

Directory.Exists() returns trueFileUpload1.SaveAs() return 我提到的相同错误。
注意:更新#1
我什至尝试了以下代码,但错误是相同的:

 var path = Server.MapPath("~/upload/" + FileUpload1.PostedFile.FileName);
 FileUpload1.PostedFile.SaveAs(path);

大多数问题的发生是因为目录不存在。
在调用 .SaveAs 之前放置此代码并再试一次:

if (!Directory.Exists(Server.MapPath("upload")))
{
    Directory.CreateDirectory(Server.MapPath("upload"));
}

更新
这是完整的 Button1_Click 事件:

protected void Button1_Click(object sender, EventArgs e)
{
    if ((FileUpload1.PostedFile != null) && (FileUpload1.PostedFile.ContentLength > 0))
    {
        string fn = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
        string SaveLocation = Server.MapPath("upload") + "\" + fn;
        try
        {
            if (!Directory.Exists(Server.MapPath("upload")))
            {
                Directory.CreateDirectory(Server.MapPath("upload"));
            }
            FileUpload1.PostedFile.SaveAs(SaveLocation);
            FileUploadStatus.Text = "The file has been uploaded.";
        }
        catch (Exception ex)
        {
            FileUploadStatus.Text = "Error: " + ex.Message;
        }
    }
    else
    {
        FileUploadStatus.Text = "Please select a file to upload.";
    }
}

使用如下扩展名保存文件。

protected void Button1_Click(object sender, EventArgs e)
    {
        if ((FileUpload1.PostedFile != null) && (FileUpload1.PostedFile.ContentLength > 0))
        {
            string fn = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
            string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
            string SaveLocation = Server.MapPath("~/upload/");
            try
            {
                FileUpload1.PostedFile.SaveAs(SaveLocation+fn +ext);
                FileUploadStatus.Text = "The file has been uploaded.";
            }
            catch (Exception ex)
            {
                FileUploadStatus.Text = "Error: " + ex.Message;
            }
        }
        else
        {
            FileUploadStatus.Text = "Please select a file to upload.";
        }
    }

您的代码是正确的,我想您在 windows 10 上的 windows defender 阻止了 IIS 访问,因为它是新功能 controlled folder access
解决您的问题:
1.开启windows防御者
2. 单击 virus and threat protection 选项卡上的 manage ransomware protection
3. 然后关闭 controlled folder access.
如果您还有问题,请告诉我。