Winform to ASPX Request.files.Allkeys is always zero

Bill Penrose 1 Reputation point
2021-01-14T21:41:48.767+00:00

I have seen this question many times on the web but never with simple code and clear answers
Below is my code of the winForm and the webASPX with no extraneous code
I am sure I am missing something
I am trying to send a file from a WinForm program to a web server and there are never any files
I get the beep but it never hits the stop
Request count is always zero

`

https://stackoverflow.com/questions/65666644/vb-net-uploadfile-request-files-allkeys-always-emptyWinForms code

Imports System.Net

Public Class Form1
Public ServerURL As String = "http://localhost:50918"

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim myWebClient As New WebClient
    Dim responseArray As Byte()
    Dim Filename As String = "C:\Temp\myfile.txt"

    responseArray = myWebClient.UploadFile(ServerURL & "/Webform1.aspx", Filename)

End Sub

End Class

Web Code

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication6.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<div>
</div>
</form>
</body>
</html>

Public Class WebForm1
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Beep()
    Debug.Print(System.Web.HttpContext.Current.Request.Files.Count & " " & System.Web.HttpContext.Current.Request.Files.AllKeys.Count)
    For Each f As String In System.Web.HttpContext.Current.Request.Files.AllKeys
        Stop
    Next

End Sub

End Class

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,303 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 112.9K Reputation points
    2021-01-15T11:05:55.793+00:00

    If .aspx does not work, then try writing an .ashx handler. Add a new item to your Web application — “Generic Handler”, Handler1.ashx.

    The simplest implementation is:

    Public Class Handler1
        Implements System.Web.IHttpHandler
    
        Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
    
            Dim c1 = context.Request.Files.Count
            Dim c2 = context.Request.Files.AllKeys.Count
            context.Request.Files(0).SaveAs("C:\Temp\ReceivedFile.txt")
    
    
            context.Response.ContentType = "text/plain"
            context.Response.Write("Hello World!")
    
        End Sub
    
        ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
            Get
                Return False
            End Get
        End Property
    
    End Class
    

    Instead of Beep and Debug.Print you can put breakpoints and investigate the variables using Debugger.

    Also adjust the client code:

    responseArray = myWebClient.UploadFile(ServerURL & "/Handler1.ashx", Filename)
    

  2. Bill Penrose 1 Reputation point
    2021-01-19T00:45:10.607+00:00

    Thank you Viorel
    That worked perfectly
    The other version should have worked according to others but this is fine
    As I understand it, the Handler is a non visible page allowing it to respond but not show anything at the web site
    regards
    Bill