Błąd podczas przesyłania pliku do kontrolera za pomocą JQuery

0

Witam mam pewien problem.
Próbuję przesłać trochę danych za pomocą ajaxa wraz z plikiem graficznym ale z jakiegoś powodu wszystkie dane textowe przesyłają się normalnie ale plik nie i mam do was pytanie co tu może być błędem i jak to naprawić?

Skrypt do przesyłania danych

let formData = new FormData();
        formData.append('file', document.querySelector('#file').files[0]);

        let fileName = document.getElementById("name").value;
        let amount = document.getElementById("amount").value;
        let price = document.getElementById("price").value;

        let data = { id: 0, name: fileName, amount: parseInt(amount), price: parseInt(price), file: formData, item_count: 0 };


        $.post('https://localhost:5001/Admin/AddProduct', data, function (res) {

        }).done(() => {
            //reset inputs
            document.querySelector("#name").value = "";
            document.querySelector("#amount").value = "";
            document.querySelector("#price").value = "";

            //successful text
            document.getElementById('successAddItem').innerHTML = "You succesfuly add " + fileName;
            setTimeout(() => { document.getElementById('successAddItem').innerHTML = ""; }, 15000);
            console.log("success");
        });

Kontroler

public async Task<ActionResult> AddProduct(ProductModel m)
        {
            //return Ok(new { m.file });
            await _productRepository.AddProduct(m.name, m.amount, m.price, m.file, _hostingEnvironment.WebRootPath);
            return new EmptyResult();
        }

Model

public int id { get; set; }
        public string name { get; set; }
        public int amount { get; set; }
        public int price { get; set; }

        public IFormFile file { get; set; }

        public int item_count { get; set; }
0

A ten plik to skąd pobierasz?
Bo JS-em możesz przesłać jako plik albo zbiór danych wygenerowanych przez samą stronę, albo plik który wcześniej użytkownik wybrał ręcznie z dysku.

0

@Freja Draco: Plik pobieram za pomocą inputa

                    <div class="col-sm-8">

                        <div class="text-center mt-2">
                            <h2><b>Add Product</b></h2>
                        </div>

                        <div class="mt-4">
                            <label for="add_product">
                                <h5>Product name</h5>
                            </label>
                            <input class="form-control" type="text" name="name" id="name" placeholder="Product name">
                        </div>

                        <div class="mt-4">
                            <label for="add_product">
                                <h5>Amount</h5>
                            </label>
                            <input class="form-control" type="number" name="amount" id="amount" placeholder="Amount">
                        </div>

                        <div class="mt-4">
                            <label for="add_product">
                                <h5>Price</h5>
                            </label>
                            <input class="form-control" type="number" name="price" id="price" placeholder="Price">
                        </div>

                        <div class="mt-4">
                            <label for="add_product">
                                <h5>Add Image</h5>
                            </label>
                            <input class="form-control-file" type="file" name="file" id="file">
                        </div>

                        <div class="text-center mt-5">
                            <button class="btn btn-success" type="submit" onclick="AddProduct()">Submit</button>
                        </div>

                        <div class="text-center text-success mt-4">
                            <b id="successAddItem">

                            </b>
                        </div>

                    </div>

dokładnie z tego fragmentu (dokładnie z fragmentu gdzie jest input file) jest input ktory jest odpowiedzialny za pobioeranie pliku

1

Mieszasz różne typy. Tworzysz FormData, a potem osadzasz go w JSONie. Przy konwersji wyjdzie z tego jakiś śmieć (pewnie [object Object]). Umieść wszystkie dane w formData i to wysyłaj. Na .NET się nie znam, ale prawdopodobnie jest na tyle bystre, żeby sobie to samo ogarnąć.

1 użytkowników online, w tym zalogowanych: 0, gości: 1