Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在c中检索字符串formData js#_Javascript_C#_Asp.net Web Api_Form Data - Fatal编程技术网

Javascript 如何在c中检索字符串formData js#

Javascript 如何在c中检索字符串formData js#,javascript,c#,asp.net-web-api,form-data,Javascript,C#,Asp.net Web Api,Form Data,我必须在.net的web api中检索“idPerson”的值。 我已经检索到文件“UploadeImage”。但我无法找回“idPerson”的价值 有人有办法吗 谢谢 我的js函数 /** * Upload de l'image de profil * @method uploadFile * @private */ uploadFile: function () { va

我必须在.net的web api中检索“idPerson”的值。 我已经检索到文件“UploadeImage”。但我无法找回“idPerson”的价值

有人有办法吗

谢谢

我的js函数

        /**
        * 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;
        },
我的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("");
        }
    }
//
///患者照片上传方法
/// 
///图像收费表
public MessageOutCoreVm UploadImg()
{
string fileSavePath=string.Empty;
字符串virtualDirectoryImg=“UploadedFiles”;
字符串文件名=string.Empty;
if(HttpContext.Current.Request.Files.AllKeys.Any())
{
//从文件集合中获取上载的图像
var httpPostedFile=HttpContext.Current.Request.Files[“uploadeImage”];
fileName=httpPostedFile.fileName;
if(httpPostedFile!=null)
{
//费希尔入口通道
fileSavePath=Path.Combine(HttpContext.Current.Server.MapPath(“~/UploadedFiles”),httpPostedFile.FileName);
//Sauvegarde du fichier dans上传的文件
httpPostedFile.SaveAs(fileSavePath);
}
返回消息outcorevm.SendSucces(virtualDirectoryImg+'/'+文件名);
}
其他的
{
返回消息OutCoreVM.SendValidationFailed(“”);
}
}

假设您发送的是典型的Ajax POST请求,您可以从
HttpContext.Current.request.Form
集合中检索每个字段

只需在集合中找到您的密钥,如
HttpContext.Current.Request.Form[“key”]

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

请求。表单[“KEY”]在我的MVC控制器中工作

Html:

<input id="image-file" type="file" onchange="SavePhoto(this)"/>
<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>
    [HttpPost]
    public void Image2()
    {
        HttpPostedFileBase img2 = Request.Files["photo"];
        string path = @"D:\Server\Image\Newred\" + img2.FileName;
        img2.SaveAs(path);
    }