我正在嘗試從連接到同一網路的另一臺設備將檔案上傳到我的計算機。我已經可以在智能手機上查看和下載計算機上的檔案,但我仍然不知道如何將檔案從智能手機上傳到我的計算機。
我在我的計算機上的 Windows 10 上使用 IIS 通過私有 ip 托管檔案(因此它可以在同一網路上作業)
我在https://docs.microsoft.com/en-us/troubleshoot/上找到了上傳檔案的示例aspnet/upload-file-to-web-site,但是當我嘗試實作它時,它給了我錯誤,它無法加載 type '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;
}
}
}
uj5u.com熱心網友回復:
您可以嘗試將Codebehind更改 為CodeFile
<%@ Page language="c#" CodeFile="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="WebForm1" %>
通常遵循網站
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/338685.html
上一篇:C#日期時間和字串值條件檢查
