简体   繁体   中英

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

I want to upload files like images using asp.net application webforms .
The application .netfaramwork is 4.6.1 .
I don't have any problem with website projects but webforms throw unusual errors.
This is my aspx page code:

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

and in code behind aspx.cs is:

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

I already create a folder in application root named it upload .
在此处输入图片说明
But when I click on the button after choosing the file from my system, it throws an error as following:

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

logo.png is the file I choose from my system. I even Try this code:

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

Directory.Exists() returns true but FileUpload1.SaveAs() return same error I mentioned.
Note: Update#1
I even try following code, but the error is same:

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

Most of all problem occurs because of non-existed directory.
Place this code before calling .SaveAs and give it a one more try:

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

Update
Here is complete Button1_Click event:

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

Your code is correct and I suppose your windows defender on windows 10 block the IIS access due to it's new feature controlled folder access .
To resolve your problem:
1. open the windows defender.
2. click on manage ransomware protection on virus and threat protection tab.
3. then switch off controlled folder access .
Tell me if you have still problem.
在此处输入图片说明

Save the file with extension like below.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM