Create Generic HTTPHandler in asp.net with multiple values using AJAX call

喜夏-厌秋 提交于 2019-12-13 02:37:29

问题


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:


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


来源:https://stackoverflow.com/questions/24029636/create-generic-httphandler-in-asp-net-with-multiple-values-using-ajax-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!