如何取回字符串FORMDATA在C#中的js [英] How can retrieve string formData js in c#

查看:558
本文介绍了如何取回字符串FORMDATA在C#中的js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.NET中我的web API来检索idPerson的值。
我已经检索文件UploadedImage。但我不能检索idPerson的值。

有人有办法解决吗?

THX!

我的js函数

  / **
        *上传DE L'图像德PROFIL
        * @method uploadFile
        * @私人的
        * /
        uploadFile:功能(){
            VAR数据=新FORMDATA(),文件,ajaxRequest;            文件= $(#文件上传)得到(0).files。            // Ajout DE L'图像uploadéVERS莱最近搜索杜形式
            如果(files.length大于0){
                data.append(UploadedImage文件[0]);
                // Ajout DE L'ID杜病人倒calculer乐GUID
                data.append(idPerson,this.vm.idPerson);
            }            返回的数据;
        },

我的Web API:

  ///<总结>
    /// METHODE D'上传德拉照片德PROFIL杜患者
    ///< /总结>
    ///<退货和GT;行政法院杜téléchargementDE L'图像和LT; /回报>
    公共MessageOutCoreVm UploadImg()
    {
        字符串fileSavePath =的String.Empty;
        字符串virtualDirectoryImg =UPLOADEDFILES;
        字符串文件名=的String.Empty;        如果(HttpContext.Current.Request.Files.AllKeys.Any())
        {
            //从文件收集获取上传的图片
            VAR httpPostedFile = HttpContext.Current.Request.Files [UploadedImage];
            文件名= httpPostedFile.FileName;            如果(httpPostedFile!= NULL)
            {
                // OBtient乐路杜fichier
                fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath(〜/ UPLOADEDFILES),httpPostedFile.FileName);                // Sauvegarde杜fichier丹斯UPLOADEDFILES河畔乐serveur
                httpPostedFile.SaveAs(fileSavePath);
            }            返回MessageOutCoreVm.SendSucces(virtualDirectoryImg +'/'+文件名);
        }
        其他
        {
            返回MessageOutCoreVm.SendValidationFailed();
        }
    }


解决方案

假设你要发送的典型的Ajax POST请求,您可以从 HttpContext.Current.Request.Form 集合。

只要找到你的收藏,比如 HttpContext.Current.Request.Form键[钥匙]

TBH这是很难说如何检索任何值,当你没有提供发送数据的方式。

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

解决方案

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.

这篇关于如何取回字符串FORMDATA在C#中的js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆