Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7798213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:56:13+00:00 2026-06-01T23:56:13+00:00

I am trying to upload images using generic handler as shown below and I

  • 0

I am trying to upload images using generic handler as shown below and I have a normal aspx page where I am showing all the uploaded images after uploading.Everything is working fine.

<%@ WebHandler Language="VB" Class="Upload"%>

Imports System
Imports System.Web
Imports System.Threading 
Imports System.Web.Script.Serialization
Imports System.IO

Public Class Upload : Implements IHttpHandler, System.Web.SessionState.IRequiresSessionState 
    Public Class FilesStatus
        Public Property thumbnail_url() As String
        Public Property name() As String
        Public Property url() As String
        Public Property size() As Integer
        Public Property type() As String
        Public Property delete_url() As String
        Public Property delete_type() As String
        Public Property [error]() As String
        Public Property progress() As String
    End Class
    Private ReadOnly js As New JavaScriptSerializer()
    Private ingestPath As String

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest


            Dim r = context.Response
            ingestPath = context.Server.MapPath("~/UploadedImages/")

            r.AddHeader("Pragma", "no-cache")
            r.AddHeader("Cache-Control", "private, no-cache")

            HandleMethod(context)
    End Sub
    Private Sub HandleMethod(ByVal context As HttpContext)
        Select Case context.Request.HttpMethod
            Case "HEAD", "GET"
                ServeFile(context)

            Case "POST"
                UploadFile(context)

            Case "DELETE"
                DeleteFile(context)

            Case Else
                context.Response.ClearHeaders()
                context.Response.StatusCode = 405
        End Select
    End Sub
    Private Sub DeleteFile(ByVal context As HttpContext)
        Dim filePath = ingestPath & context.Request("f")
        If File.Exists(filePath) Then
            File.Delete(filePath)
        End If
    End Sub
    Private Sub ServeFile(ByVal context As HttpContext)
        If String.IsNullOrEmpty(context.Request("f")) Then
            ListCurrentFiles(context)
        Else
            DeliverFile(context)
        End If
    End Sub

    Private Sub UploadFile(ByVal context As HttpContext)
        Dim statuses = New List(Of FilesStatus)()
        Dim headers = context.Request.Headers

        If String.IsNullOrEmpty(headers("X-File-Name")) Then
            UploadWholeFile(context, statuses)
        Else
            UploadPartialFile(headers("X-File-Name"), context, statuses)
        End If


        WriteJsonIframeSafe(context, statuses)
    End Sub

    Private Sub UploadPartialFile(ByVal fileName As String, ByVal context As HttpContext, ByVal statuses As List(Of FilesStatus))
        If context.Request.Files.Count <> 1 Then
            Throw New HttpRequestValidationException("Attempt to upload chunked file containing more than one fragment per request")
        End If
        Dim inputStream = context.Request.Files(0).InputStream
        Dim fullName = ingestPath & Path.GetFileName(fileName)

        Using fs = New FileStream(fullName, FileMode.Append, FileAccess.Write)
            Dim buffer = New Byte(1023) {}

            Dim l = inputStream.Read(buffer, 0, 1024)
            Do While l > 0
                fs.Write(buffer, 0, l)
                l = inputStream.Read(buffer, 0, 1024)
            Loop
            fs.Flush()
            fs.Close()
        End Using

        statuses.Add(New FilesStatus With {.thumbnail_url = "Thumbnail.ashx?f=" & fileName, .url = "Upload.ashx?f=" & fileName, .name = fileName, .size = CInt((New FileInfo(fullName)).Length), .type = "image/png", .delete_url = "Upload.ashx?f=" & fileName, .delete_type = "DELETE", .progress = "1.0"})

    End Sub

    Private Sub UploadWholeFile(ByVal context As HttpContext, ByVal statuses As List(Of FilesStatus))
        For i As Integer = 0 To context.Request.Files.Count - 1
            Dim file = context.Request.Files(i)
            file.SaveAs(ingestPath & Path.GetFileName(file.FileName))
            Thread.Sleep(1000)
            Dim fname = Path.GetFileName(file.FileName)
            statuses.Add(New FilesStatus With {.thumbnail_url = "Thumbnail.ashx?f=" & fname, .url = "Upload.ashx?f=" & fname, .name = fname, .size = file.ContentLength, .type = "image/png", .delete_url = "Upload.ashx?f=" & fname, .delete_type = "DELETE", .progress = "1.0"})
        Next i
    End Sub

    Private Sub WriteJsonIframeSafe(ByVal context As HttpContext, ByVal statuses As List(Of FilesStatus))
        context.Response.AddHeader("Vary", "Accept")
        Try
            If context.Request("HTTP_ACCEPT").Contains("application/json") Then
                context.Response.ContentType = "application/json"
            Else
                context.Response.ContentType = "text/plain"
            End If
        Catch
            context.Response.ContentType = "text/plain"
        End Try

        Dim jsonObj = js.Serialize(statuses.ToArray())
        context.Response.Write(jsonObj)
    End Sub
    Private Sub DeliverFile(ByVal context As HttpContext)
        Dim filePath = ingestPath & context.Request("f")
        If File.Exists(filePath) Then
            context.Response.ContentType = "application/octet-stream"
            context.Response.WriteFile(filePath)
            context.Response.AddHeader("Content-Disposition", "attachment, filename=""" & context.Request("f") & """")
        Else
            context.Response.StatusCode = 404
        End If
    End Sub
    Private Sub ListCurrentFiles(ByVal context As HttpContext)
        Dim files = New List(Of FilesStatus)()

        Dim names = Directory.GetFiles(context.Server.MapPath("~/UploadedImages/"), "*", SearchOption.TopDirectoryOnly)

        For Each name In names
            Dim f = New FileInfo(name)
            files.Add(New FilesStatus With {.thumbnail_url = "Thumbnail.ashx?f=" & f.Name, .url = "Upload.ashx?f=" & f.Name, .name = f.Name, .size = CInt(f.Length), .type = "image/png", .delete_url = "Upload.ashx?f=" & f.Name, .delete_type = "DELETE"})
        Next name

        context.Response.AddHeader("Content-Disposition", "inline, filename=""files.json""")
        Dim jsonObj = js.Serialize(files.ToArray())
        context.Response.Write(jsonObj)
        context.Response.ContentType = "application/json"
    End Sub
    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class

Now I want to add a session variable by generating a random string and add the uploaded images to the newly created random string.

1.I have seen this Question on SO to use System.Web.SessionState.IRequiresSessionState for sessions and how do I create a folder with that and add my images to that folder after doing that how do I access this session variable in my normal aspx page.

2.(Or) the better way is create session variable in aspx page and pass that to handler?If so how can I do that?

3 .I am trying to find the control from my handler.Is that possible?If anyone knows how to get this then also my problem will get resolved so that I am trying to create a session from m aspx page.

Can anyone explain the better way of handling this situation.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:56:15+00:00Added an answer on June 1, 2026 at 11:56 pm

    I completely agree with jbl‘s comment.

    1. You can get and set session using HttpContext.Current.Session anywhere on your project.
    2. No matter where you create the session. Just make sure that the session exists before you access it.
    3. Not sure what exactly you are asking here(need some more explanation).

    Here is an example, where I used session on HttpHandler. However, it is on c#(hope you can understand).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to upload an image from a jsp page using servlet. but
I am trying to upload multiple images and I have a jquery plugin set
I am trying to upload images using a webservice from silverlight. I am first
I'm trying to upload an image using normal form for normal admin for normal
I'm trying to upload images using Graph API Batch Request, but i'm unable to
I have ~16,000 images I'm trying to upload to Amazon. Right now, they're on
I am trying to upload multiple images using meioupload which works fine if I
I'm trying to encode and upload two (or more) images from Flash using JPGEncoder.
I am trying to upload images to ImageShack using the API. I think they
I am trying to upload images on a form but I am using Jquery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.