1

I am uploading an image file to .net MVC controller using javascript and AJAX POST call. But the problem is--

1) I am getting size of file in javscript using this var FileSize = $('#ProfilePicUpload')[0].files[0].size;

When I send it to controller it doesnot receive any data in byte datatype. Instead it goes either in string or int or long datatype.

1)I've used jquery to get the size of image using files[0].size. I successfully get the size of uploaded image.

2)I also set content-type: 'application-json/UTF-8'. and processData:false and contentType:false. But this results in string[] OfficialData to 0 on controller side.

3)I am receving rest of form data in string[] on controller side and only image size in byte[] format

//OfficialData.js file

    var FileSize = $('#ProfilePicUpload')[0].files[0].size; //get size of image file.

        //ajax call
        $.ajax({
                  type: "POST",
                  url: "/Employee/AddEmployeeOfficialDetail",
                  data: { "OfficialData": Employee_Data, size: FileSize },
                  success: function (data) {
                     if (data.check === -1) {
                     jAlert("Employee Already Exists!", "Alert", function () {});}

        //controller code
        public async Task<IActionResult> AddEmployeeOfficialDetail(string[] OfficialData,byte[] size)
        {
        Employee.fileContents.FileSize = BitConverter.GetBytes(size);
        }    

I get byte[] size = null If I use int then I get size of image File like int size = 34598.

My only requirement is to get exactly the size of Image from java-script from ajax call to byte[] on controller side.

9
  • 2
    Why are you trying to use a byte array for a single number value? If you want an array you need to send an array, meaning you would have to break apart your size value into its high/low order bytes manually on the client side. Or change the type to int and do the operation on the servr Commented Jun 8, 2019 at 11:59
  • What is the value of FileSize when you console.log it? Commented Jun 8, 2019 at 12:08
  • @mjwills it gets as FileSize = 20857..It's different for different files..but I get like this when I console.log it Commented Jun 8, 2019 at 12:16
  • Have you tried int size instead? Commented Jun 8, 2019 at 12:19
  • @PatrickEvans I want byte array because I want to store it in azure blob storage..and over there it's asks content in byte[] only..that's where I'm stucked! Commented Jun 8, 2019 at 12:19

0

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.