VB.net FileUpload using Ajaxcall

2020-07-30 02:29发布

问题:

Hi i have a Jqgrid subgrid. inside that i have upload control.

upload control working fine upto asking for the file and select the file.

But i am unable to read the file value in the back end vb.net side.

Javascript code :
$('#FormPath')[0].files[0] will return [object,file].
That means it is able to read the file.

                       var fd = new FormData();    
                       fd.append('file', $('#FormPath')[0].files[0]);
                    
                       $.ajax({
                           url: 'Forms.aspx/UploadFormDetails',
                           data: fd,
                           cache:false,
                           processData: false,
                           contentType: false,
                           type: 'POST',
                           success: function(fd){
                               alert(fd);
                           }
                       });

My bad luck it is not hitting the code mentioned below. if it hits i may read the file. Vb.net Code

 <WebMethod> _
    Public Shared Function UploadFormDetails() As String

        Dim httpPostedFile = HttpContext.Current.Request.Files("UploadedFile")

        If httpPostedFile IsNot Nothing Then
            

            ' Get the complete file path
            Dim fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("/Attachments/pdf/Forms/"), httpPostedFile.FileName)

            ' Save the uploaded file to "UploadedFiles" folder
            httpPostedFile.SaveAs(fileSavePath)
        End If

       


        Return ""

    End Function

Any body have any idea

回答1:

Can be accomplished by following the instructions at this link: http://www.binaryintellect.net/articles/f2a2f1ee-e18a-416b-893e-883c800f83f4.aspx

"Instead of making a full page postback you can use jQuery to make an Ajax call to the server and POST the selected files to a generic handler (.ashx). The generic handler can then save the files to a specified folder. The remainder of this post shows how this can be accomplished."

<script type="text/javascript">
$(document).ready(function () {
  $("#Button1").click(function (evt) {
    var fileUpload = $("#FileUpload1").get(0);
    var files = fileUpload.files;

    var data = new FormData();
    for (var i = 0; i < files.length; i++) {
      data.append(files[i].name, files[i]);
    }

    var options = {};
    options.url = "FileUploadHandler.ashx";
    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);

   evt.preventDefault();
  });
});
</script>

Then in your Handler file, you can do something like this to save the POSTed files to your server.

namespace jQueryFileUploadDemo
{
    public class FileUploadHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.Files.Count > 0)
            {
                HttpFileCollection files = context.Request.Files;
                for (int i = 0; i < files.Count;i++ )
                {
                    HttpPostedFile file = files[i];
                    string fname = context.Server.MapPath("~/uploads/" + file.FileName);
                    file.SaveAs(fname);
                }
            }
            context.Response.ContentType = "text/plain";
            context.Response.Write("File(s) Uploaded Successfully!");
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}