Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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
C# 在ASP.NET MVC中上载文件_C#_Ajax_Asp.net Mvc_File Upload - Fatal编程技术网

C# 在ASP.NET MVC中上载文件

C# 在ASP.NET MVC中上载文件,c#,ajax,asp.net-mvc,file-upload,C#,Ajax,Asp.net Mvc,File Upload,我有 现在我想在Ajax中上传文件并将文件保存到另一个文件夹中。 如何从输入表单获取文件并通过Ajax发送 试试这个: cshtml: [HttpPost] public ActionResult GetFiles(HttpPostedFileBase file) { if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileNam

我有

现在我想在Ajax中上传文件并将文件保存到另一个文件夹中。 如何从输入表单获取文件并通过Ajax发送

试试这个:

cshtml:

[HttpPost]
public ActionResult GetFiles(HttpPostedFileBase file)
{
    if (file != null && file.ContentLength > 0)
    {            
        var fileName = Path.GetFileName(file.FileName);   
        var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
        file.SaveAs(path);
    }
    return Json("Uploaded " + Request.Files.Count + " files",JsonRequestBehavior.AllowGet);
}

在服务器端的SetImageData方法中完成您的任务。

正如@Reza Aghaei已经说过的,您可以使用FormData来解决您的问题,但我建议您使用ajaxForm方法。我使用这种方法是因为


使用它并不困难。

您还没有显示ajax调用。在使用ajax发布文件时,需要使用FormData。我试着说它不起作用。如果您知道工作版本,请告诉meTried什么?你们还并没有展示你们的代码,所以我们怎么能猜出你们做错了什么?我正在尝试在互联网上搜索多种形式的表单数据。但是我的FormData仍然是空的,我无法在我的控制器中获取它!
<input id="myFile" type="file" name="myfile" onchange="GoToPreview(this)" style="display: none">



function GoToPreview(input) {
            if (input.files && input.files[0]) {  

                var tmpImageData = new FileReader();
                tmpImageData.onload = function (e) {
                    SetImageData(imageData);                
                }
                tmpImageData.readAsDataURL(input.files[0]);
            }
    }

function SetImageData(imageData) {
        $.ajax({
            url: '/Home/SetImageData',
            type: 'POST',
            data: { imageData: imageData },
            success: function (data) {
                // code here
            },
            error: function (data) {
                // code for exception
            }
        });
    }