Winform to ASPX Request.files.Allkeys is always zero

Bill Penrose 1 Reputation point

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

` 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="">
<head runat="server">
<form id="form1" runat="server" enctype="multipart/form-data">

Public Class WebForm1
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    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

End Sub

End Class

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

    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.Response.ContentType = "text/plain"
            context.Response.Write("Hello World!")
        End Sub
        ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
                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

    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