无法获取 asp.net 上传文件示例以在 IIS 本地网络上工作



我正在尝试从连接到同一网络的另一台设备上传文件到我的计算机。我已经可以在智能手机上查看和下载电脑上的文件,但我仍然不知道如何从智能手机上上传文件到电脑上。
我在我的计算机上使用windows 10上的IIS通过私有ip托管文件(因此它在同一网络上工作)
我已经找到了在https://learn.microsoft.com/en-us/troubleshoot/aspnet/upload-file-to-web-site上上传文件的示例,但是当我尝试实现它时,它给了我错误,它无法加载类型'upload.WebForm1'。我删除了所有的代码,除了使用指令,空间名和类。下面是代码:

WebForm1.aspx

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="upload.WebForm1" %>

当我包括剩下的stackoverflow说红色:你的帖子似乎包含代码没有正确格式化为代码。请使用代码工具栏按钮或CTRL+K快捷键缩进所有代码4个空格。有关更多编辑帮助,请单击[?工具栏图标。不要让我提问。
.aspx文档的其余部分从上面链接的完整代码清单中复制并且没有更改。

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

你能不能试着改变CodebehindCodeFile

<%@页面语言=&quot;c#&quot;<strong> CodeFile ="WebForm1.aspx.cs&quot </strong>AutoEventWireup ="false&quot;继承="upload.WebForm1&quot;%在</p><p>通常跟在Website</p> 后面</div></html>%>

最新更新