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.

hassan.ef
  • 1,300
  • 2
  • 11
  • 19
  • 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 – Patrick Evans Jun 08 '19 at 11:59
  • What is the value of `FileSize` when you `console.log` it? – mjwills Jun 08 '19 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 – Vedang Pandya Jun 08 '19 at 12:16
  • Have you tried `int size` instead? – mjwills Jun 08 '19 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! – Vedang Pandya Jun 08 '19 at 12:19
  • @mjwills yes.. I tried `int` as well as `long`.. Here is the controller code `public async Task AddEmployeeOfficialDetail(string[] OfficialData,int size)` Now when I convert this using this `Employee.fileContents.FileSize = BitConverter.GetBytes(size);` here I get `byte[]` but when I upload in azure storage image is damaged... – Vedang Pandya Jun 08 '19 at 12:20
  • Why are you using `string[]` for an image? – mjwills Jun 08 '19 at 12:25
  • i think you are looking at the documentation wrong. Are you sure it isnt saying to store the actual file data in a byte array – Patrick Evans Jun 08 '19 at 12:30
  • @mjwills it contains other data from form...in javascript array like `var Employee_Data = {}; Employee_Data[0] = EmployeeCode;` – Vedang Pandya Jun 08 '19 at 12:37

0 Answers0