【问题标题】:Could not find file error on uploading files in asp.net application webforms在 asp.net 应用程序 webforms 中上传文件时找不到文件错误
【发布时间】:2020-04-18 16:32:06
【问题描述】:

我想使用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

但是当我从系统中选择文件后单击按钮时,会引发如下错误:

错误:找不到文件 'C:\Users\mybla\Documents\Visual Studio 2017\Projects\05.学习\fileUploadTest\fileUploadTest\upload\logo.png'。

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

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

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

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

【问题讨论】:

    标签: c# asp.net file-upload webforms


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      大多数问题是由于不存在的目录而发生的。
      在调用 .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.";
          }
      }
      

      【讨论】:

      • 不。我也试试你的代码。但正如我在使用 Directory.Exists() 测试之前所说的,我意识到该目录存在。
      • 我完全复制了你的项目,它确实使用你提供的代码抛出了这个错误,但是只要你从我的答案中添加 sn-p - 它工作正常,因为请求的文件夹现在可用。我用完成的 Button1_Click 更新了我的答案
      • 谢谢,但仍然是同样的错误。我怀疑我的 windows10 防御者,所以我将在另一个装有 windows 8.1 的系统上测试我的程序。
      • 告诉我们结果如何?
      • 我可以在 Windows 8.1 上运行,没有任何问题。
      【解决方案3】:

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

      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.";
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多