Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can retrieve string formData js in c#

I have to retrieve the value of "idPerson" in my web api in .net. I already retrieve the file "UploadedImage". But I can't retrieve the value of "idPerson".

Someone have a solution?

Thx !

my js function

        /**
        * Upload de l'image de profil
        * @method uploadFile
        * @private
        */
        uploadFile: function () {
            var data = new FormData(), files, ajaxRequest;

            files = $("#fileUpload").get(0).files;

            // Ajout de l'image uploadé vers les données du form
            if (files.length > 0) {
                data.append("UploadedImage", files[0]);
                // Ajout de l'id du patient pour calculer le GUID 
                data.append("idPerson", this.vm.idPerson);
            }

            return data;
        },

my web api :

 /// <summary>
    /// Méthode d'upload de la photo de profil du patient
    /// </summary>
    /// <returns>Etat du téléchargement de l'image</returns>
    public MessageOutCoreVm UploadImg()
    {
        string fileSavePath = string.Empty;
        string virtualDirectoryImg = "UploadedFiles";
        string fileName = string.Empty;

        if (HttpContext.Current.Request.Files.AllKeys.Any())
        {
            // Get the uploaded image from the Files collection
            var httpPostedFile = HttpContext.Current.Request.Files["UploadedImage"];
            fileName = httpPostedFile.FileName;

            if (httpPostedFile != null)
            {
                // OBtient le path du fichier 
                fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/UploadedFiles"), httpPostedFile.FileName);

                // Sauvegarde du fichier dans UploadedFiles sur le serveur
                httpPostedFile.SaveAs(fileSavePath);
            }

            return MessageOutCoreVm.SendSucces(virtualDirectoryImg + '/' + fileName);
        }
        else
        {
            return MessageOutCoreVm.SendValidationFailed("");
        }
    }
like image 294
Yvan Avatar asked May 07 '15 10:05

Yvan


People also ask

How can I retrieve data from form data?

get() The get() method of the FormData interface returns the first value associated with a given key from within a FormData object. If you expect multiple values and want all of them, use the getAll() method instead. Note: This method is available in Web Workers.

What is new FormData ()?

FormData objects are used to capture HTML form and submit it using fetch or another network method. We can either create new FormData(form) from an HTML form, or create an object without a form at all, and then append fields with methods: formData. append(name, value)


2 Answers

Assuming your are sending typical Ajax POST request, you can retrieve each field from HttpContext.Current.Request.Form collection.

Just find your key in collection like HttpContext.Current.Request.Form["KEY"]

TBH it is hard to say how to retrieve any value when you did not provide the way of sending data.

like image 172
kamil-mrzyglod Avatar answered Oct 12 '22 23:10

kamil-mrzyglod


Request.Form["KEY"] worked in my MVC controller.

like image 24
Baqer Naqvi Avatar answered Oct 12 '22 23:10

Baqer Naqvi