本文介绍了MVC3 AJax问题帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用MVC3使用Ajax上传文件,我已经将一个控制器上传到文件中.

Hi,
I am using MVC3 to upload a file using ajax,i have made a controller to upload file

 public JsonResult ChangeImage(HttpPostedFileBase file)
        {
            UserProfileModel model=new UserProfileModel();
            string path = "";
            string imgpath = "";
            string upath = ConfigurationManager.AppSettings["imagepath"];
            string thumbnail = "";
            if (file != null && file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                //fileName = Regex.Replace(fileName, "[^a-zA-Z0-9_.]+", "", RegexOptions.Compiled);
                if (IsImage(file.FileName.ToString()))
                {

                    fileName = "Image_" + Session["uid"].ToString() + "_" + fileName;
                    path = Path.Combine(Server.MapPath(upath), fileName);
                    file.SaveAs(path);
                    imgpath = fileName;
                    ViewData["img"] = imgpath;
                        }
                    }

                }
}



并在我的cshtml文件中使用此代码



and on my cshtml file i''m using this code

function UpdateAvailableDate(img) {
       $.post("/UserProfile/ChangeImage", { "img": img },
                           function (data) {
                               if (data == 'true') {
                                   alert('Success');

                               }
                               else
                                   alert('Failed');
                           });

   }

   $('#updnewimg').live('click', function () {
       alert('hi');
       var img = $('#newimg').val();
       if (img == null || img == "") {
           alert('Select an image');
       }
       else {
           // itemId = $(this).closest('#posted-book').find('#HiddenId')[0].value;
           UpdateAvailableDate(img);
       }
   });



newimg是我的上传控件的ID



newimg is the id of my upload control

<input type="file" id="newimg">



而updnewimg是我提交表单的按钮



and updnewimg is my button to submit form

<input type="button" id="updnewimg">



问题在于,当涉及controller(ChangeImage)时,文件的值显示为null,即控制器的参数显示为null

谁能为这个问题提供解决方案?
关于



The problem is that, when it comes to controller(ChangeImage) the value of file appears as null ie the parameter of the controller is appearing null

Can anyone gives solution for this problem?
Regards

推荐答案




这篇关于MVC3 AJax问题帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 20:10