Home > Back-end >  How can I combine iFormFile with string at the same time?
How can I combine iFormFile with string at the same time?

Time:06-14

I was trying to save an image with given folder name using string on controller, but It always shows null for folder. I know it's because of the processData in ajax but how do I use the two commands at the same time? Here's what I tried so far

event.preventDefault();
    var files = $("#image")[0].files;
    var folder = $("#type").val();
    var formData = new FormData();
    formData.append("image", files[0]);

    $.ajax({
        type: "POST",
        url: "/Admin/Fileszxc",
        data: formData,
        processData: false,
        contentType: false,
    })

Trying to add the string value

    $.ajax({
        type: "POST",
        url: "/Admin/Fileszxc",
        data: formData,
        processData: false,
        contentType: false,
    }).then(() => {
        type: "POST",
        url: "/Admin/Fileszxc",
        data: {
           folder: folder
        },
        success: function (result) {
           $("#type").val(result[1]) //using [1] because the image is [0]
        }
    })

Tried these too:

        type: "POST",
        url: "/Admin/Fileszxc",
        data: formData,
        processData: false,
        contentType: false,
        data: {
        folder: folder
        },
        success: function (result) {
           $("#type").val(result[1]) //using [1] because the image is [0]
        }

These is my controller looks like:

public async Task<IActionResult> Fileszxc(IFormFile image, string folder)
    {
        if (folder == "ACCESSORIES")
        {
            folder = "acce-image";
        } else if (folder == "CLOCKS")
        {
            folder = "clocks-image";
        } else if (folder == "COOKING")
        {
            folder = "cooking-image";
        } else if (folder == "FURNITURE")
        {
            folder = "furniture-image";
        } else if (folder == "JEWELRY")
        {
            folder = "jewelry-image";
        } else if (folder == "LIGHTING")
        {
            folder = "lighting-image";
        } else if(folder == "TOYS")
        {
            folder = "toys-image";
        }

        string path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/"   folder);

        if (!Directory.Exists(path))
            Directory.CreateDirectory(path);

        string fileName = image.FileName;

        string fileNameWithPath = Path.Combine(path, fileName);
        using (var stream = new FileStream(fileNameWithPath, FileMode.Create))
        {
            image.CopyTo(stream);
        }
        return View();
    }

Any hint or given answer will be appreciated.

CodePudding user response:

For pass file with other string parameter, you need use the following code:

var files = $("#image")[0].files;
var folder = $("#type").val();
var formData = new FormData();
formData.append("image", files[0]);
formData.append("folder", folder);        //add this...

$.ajax({
    type: "POST",
    url: "/Admin/Fileszxc",
    data: formData,
    processData: false,
    contentType: false,
})
  • Related