单击MVC视图中的按钮后,将执行以下javascript函数
<script type="text/javascript">
function showAndroidUpload(string) {
Android.AndroidUpload(string);
var url = '@Url.Action("TestMove","Functions")';
$.ajax({ url: url, success: DataRetrieved, type: 'POST', dataType: 'json' });
}
</script>AndroidUpload函数是一个在我的安卓设备上运行的javascript函数,它将图像上传到我的~/App_Data/文件夹中,我希望将该图像移动到我的~/Content/images/文件夹中。我在控制器中的操作如下所示:
public ActionResult TestMove()//UploadModel model)//, IEnumerable<HttpPostedFileBase> picture)
{
string UploadedPath = "~/App_Data/image.jpg";
string SavePath = "~/Content/images/movedimage.jpg";
System.IO.File.Move(UploadedPath, SavePath);
return RedirectToAction("Index");
}图像上传可以正常工作,但永远不会执行该操作。这是使用ajax调用它的正确方式吗?
我知道我的文件名等是正确的,所以我不确定问题出在哪里。
发布于 2013-04-24 12:14:53
编辑:我之前的答案,虽然在技术上是正确的,但不是最好的。感谢BASmith为我指明了正确的方向。
EDIT2:添加了来自ajax调用的重定向逻辑。
未调用您的操作,因为TestMove方法不是此类的成员:
public class FunctionsController : Controller
{
}因此,可以通过以下两种方式之一来解决此问题:
FunctionsController : Controller类并向其中添加TestMove方法。"Functions"参数更改为TestMove方法当前所在的控制器的名称。由于您是通过ajax调用方法的,因此您需要自己处理重定向,如下所示:MVC RedirectToAction through ajax jQuery call in knockoutjs is not working
JavaScript:
<script type="text/javascript">
function showAndroidUpload(string) {
Android.AndroidUpload(string);
var url = '@Url.Action("TestMove","Functions")';
$.ajax({ url: url, success: function(response){ window.location.href = response.Url; }, type: 'POST', dataType: 'json' });
}
</script>控制器:
public ActionResult TestMove()//UploadModel model)//, IEnumerable<HttpPostedFileBase> picture)
{
string UploadedPath = "~/App_Data/image.jpg";
string SavePath = "~/Content/images/movedimage.jpg";
System.IO.File.Move(UploadedPath, SavePath);
var redirectUrl = new UrlHelper(Request.RequestContext).Action("Index", "Home");
return Json(new { Url = redirectUrl });
}https://stackoverflow.com/questions/16182988
复制相似问题