1

I want to create File Upload Handler in VB.NET.

What I want to do is pass some 5 values and a file uploaded in file upload control through AJAX call using jQuery.

But my problem is I don't know how to pass these values + file to ASHX and how to retrieve values in Generic HTTPHandler.

What I've done yet is:

var fileType = '#' + $('input[type=file]')[0].id;

if (fileType != null && typeof(fileType)!='undefined')
{
    document.getElementById('hdnFileName').value = $(fileType).val().split('\\').pop();
    //document.getElementById('hdnFileName').value = $(fileType).val();

    var files = fileUpload.files;

    var data = new FormData();
    for (var i = 0; i < files.length; i++) {
      data.append(files[i].name, files[i]);
    }
    data.append(workspaceId, document.getElementById(HFWorkspaceId).value());
    var options = {};
    options.url = "FileUploadHandler.aspx";
    options.type = "POST";
    options.data = data;
    options.contentType = false;
    options.processData = false;
    options.success = function (result) { alert(result); };
    options.error = function (err) { alert(err.statusText); };

    $.ajax(options);

    $(fileType).attr('disabled','disabled');
}

1 Answer 1

1

Yes, I've found my way.

The error was - I was writing a code

data.append(myname, document.getElementById(txtMyName).value());

while right way is -

data.append("myname", document.getElementById(txtMyName).value());

and its done.

And for Generic HTTP Handler, just ad it from Add New Item and then

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

Imports System
Imports System.Web

Public Class FileUploadHandler : Implements IHttpHandler

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

        Dim myName As String = String.Empty

        Try

            myName = context.Request.Form("myName")


            If context.Request.Files.Count > 0 Then
            Dim files As HttpFileCollection = context.Request.Files
                For i As Integer = 0 To files.Count - 1
                        Dim file As HttpPostedFile = files(i)
                        Dim fname As String = context.Server.MapPath(System.Configuration.ConfigurationManager.AppSettings("FolderForSave File")) + myName.ToString.Trim() + "/" +  file.FileName
                        file.SaveAs(fname)
                    Next
                End If

            End If

            context.Response.ContentType = "text/plain"
        Catch ex As Exception
            context.Response.ContentType = "text/plain"
            'ScriptManager.RegisterStartupScript(Me.ProcessRequest,Me.GetType,"","alert('Error Occured in')"
        End Try
    End Sub

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.