Home > Blockchain >  Can't get asp.net upload file example to work on IIS local network
Can't get asp.net upload file example to work on IIS local network

Time:10-28

I'm trying to upload files to my computer from another device connected to the same network. I already can view and download files from my computer on my smartphone, but I still don't know how can I upload files from my smartphone to my computer.
I'm using IIS on windows 10 on my computer to host files over private ip (so it works on the same network)
I have found example to upload files on https://docs.microsoft.com/en-us/troubleshoot/aspnet/upload-file-to-web-site , but when I try to implement it, it gives me error, that it can't load type 'upload.WebForm1' . I have deleted all the code except using directives, spacename and class. Here is the code:

WebForm1.aspx

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

When I include the rest stackoverflow says red: Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL K keyboard shortcut. For more editing help, click the [?] toolbar icon. and not let me post question.
Rest of .aspx document is copied and not changed from complete code listing from the link above.

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
{

}
}

Any ideas what's wrong?


Working code:

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

CodePudding user response:

Can you try changing Codebehind to CodeFile

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

Usually follows for Website

  • Related