How can retrieve string formData js in c#

北城余情 提交于 2019-12-19 03:22:23

问题


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("");
        }
    }

回答1:


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.




回答2:


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




回答3:


Html:

<input id="image-file" type="file" onchange="SavePhoto(this)"/>

JavaScript:

<script type="text/javascript">

function SavePhoto()
{
     var photo = document.getElementById("image-file").files[0];  // file from input
     var req = new XMLHttpRequest();
     var formData = new FormData();

    formData.append("photo", photo );
    req.open("POST", '/upload/image2/');
    req.send(formData);       
}
</script>

C# Controller:

    [HttpPost]
    public void Image2()
    {
        HttpPostedFileBase img2 = Request.Files["photo"];
        string path = @"D:\Server\Image\Newred\" + img2.FileName;
        img2.SaveAs(path);
    }


来源:https://stackoverflow.com/questions/30099078/how-can-retrieve-string-formdata-js-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!