无法使 asp.net 上传文件示例在 IIS 本地网络上工作
Can't get asp.net upload file example to work on IIS local network
我正在尝试将文件从连接到同一网络的另一台设备上传到我的计算机。我已经可以在智能手机上查看和下载计算机中的文件,但我仍然不知道如何将智能手机中的文件上传到计算机。
我在我的计算机上使用 windows 10 上的 IIS 通过私有 ip 托管文件(因此它可以在同一网络上工作)
我找到了在 https://docs.microsoft.com/en-us/troubleshoot/aspnet/upload-file-to-web-site 上上传文件的示例,但是当我尝试实现它时,它给了我错误,它无法加载类型 'upload.WebForm1'
。我删除了除使用指令、spacename 和 class 之外的所有代码。这是代码:
WebForm1.aspx
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="upload.WebForm1" %>
当我包含其余部分时,Whosebug 显示红色:您的 post 似乎包含未正确格式化为代码的代码。请使用代码工具栏按钮或 CTRL+K 键盘快捷键将所有代码缩进 4 个空格。如需更多编辑帮助,请单击 [?] 工具栏图标。而且不让我 post 提问。
.aspx 文档的其余部分是从上面 link 的完整代码清单中复制出来的,没有更改。
WebForm1.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
namespace upload {
public class WebForm1 : System.Web.UI.Page
{
}
}
有什么问题吗?
工作代码:
WebForm1.aspx
<%@ Page language="c#" CodeFile="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="upload.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server" EncType="multipart/form-data" action="WebForm1.aspx">
Image file to upload to the server: <INPUT id="oFile" type="file" runat="server" NAME="oFile">
<asp:button id="btnUpload" type="submit" text="Upload" runat="server"></asp:button>
<asp:Panel ID="frmConfirmation" Visible="False" Runat="server">
<asp:Label id="lblUploadResult" Runat="server"></asp:Label>
</asp:Panel>
</form>
</body>
</HTML>
WebForm1.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
namespace upload {
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public partial class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnUpload_Click(object sender, System.EventArgs e)
{
string strFileName;
string strFilePath;
string strFolder;
strFolder = Server.MapPath("./");
// Get the name of the file that is posted.
strFileName = oFile.PostedFile.FileName;
strFileName = Path.GetFileName(strFileName);
if(oFile.Value != "")
{
// Create the directory if it does not exist.
if(!Directory.Exists(strFolder))
{
Directory.CreateDirectory(strFolder);
}
// Save the uploaded file to the server.
strFilePath = strFolder + strFileName;
if(File.Exists(strFilePath))
{
lblUploadResult.Text = strFileName + " already exists on the server!";
}
else
{
oFile.PostedFile.SaveAs(strFilePath);
lblUploadResult.Text = strFileName + " has been successfully uploaded.";
}
}
else
{
lblUploadResult.Text = "Click 'Browse' to select the file to upload.";
}
// Display the result of the upload.
frmConfirmation.Visible = true;
}
}
}
你能试试把 Codebehind 改成 CodeFile
<%@ Page language="c#" CodeFile="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="upload.WebForm1" %>
通常关注网站
我正在尝试将文件从连接到同一网络的另一台设备上传到我的计算机。我已经可以在智能手机上查看和下载计算机中的文件,但我仍然不知道如何将智能手机中的文件上传到计算机。
我在我的计算机上使用 windows 10 上的 IIS 通过私有 ip 托管文件(因此它可以在同一网络上工作)
我找到了在 https://docs.microsoft.com/en-us/troubleshoot/aspnet/upload-file-to-web-site 上上传文件的示例,但是当我尝试实现它时,它给了我错误,它无法加载类型 'upload.WebForm1'
。我删除了除使用指令、spacename 和 class 之外的所有代码。这是代码:
WebForm1.aspx
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="upload.WebForm1" %>
当我包含其余部分时,Whosebug 显示红色:您的 post 似乎包含未正确格式化为代码的代码。请使用代码工具栏按钮或 CTRL+K 键盘快捷键将所有代码缩进 4 个空格。如需更多编辑帮助,请单击 [?] 工具栏图标。而且不让我 post 提问。
.aspx 文档的其余部分是从上面 link 的完整代码清单中复制出来的,没有更改。
WebForm1.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
namespace upload {
public class WebForm1 : System.Web.UI.Page
{
}
}
有什么问题吗?
工作代码:
WebForm1.aspx
<%@ Page language="c#" CodeFile="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="upload.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server" EncType="multipart/form-data" action="WebForm1.aspx">
Image file to upload to the server: <INPUT id="oFile" type="file" runat="server" NAME="oFile">
<asp:button id="btnUpload" type="submit" text="Upload" runat="server"></asp:button>
<asp:Panel ID="frmConfirmation" Visible="False" Runat="server">
<asp:Label id="lblUploadResult" Runat="server"></asp:Label>
</asp:Panel>
</form>
</body>
</HTML>
WebForm1.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
namespace upload {
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public partial class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void btnUpload_Click(object sender, System.EventArgs e)
{
string strFileName;
string strFilePath;
string strFolder;
strFolder = Server.MapPath("./");
// Get the name of the file that is posted.
strFileName = oFile.PostedFile.FileName;
strFileName = Path.GetFileName(strFileName);
if(oFile.Value != "")
{
// Create the directory if it does not exist.
if(!Directory.Exists(strFolder))
{
Directory.CreateDirectory(strFolder);
}
// Save the uploaded file to the server.
strFilePath = strFolder + strFileName;
if(File.Exists(strFilePath))
{
lblUploadResult.Text = strFileName + " already exists on the server!";
}
else
{
oFile.PostedFile.SaveAs(strFilePath);
lblUploadResult.Text = strFileName + " has been successfully uploaded.";
}
}
else
{
lblUploadResult.Text = "Click 'Browse' to select the file to upload.";
}
// Display the result of the upload.
frmConfirmation.Visible = true;
}
}
}
你能试试把 Codebehind 改成 CodeFile
<%@ Page language="c#" CodeFile="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="upload.WebForm1" %>
通常关注网站