大家好日子,
我正在尝试使用ajax从客户端到服务器端(asp.net核心)控制器上传文件,但我的值为空。
这是我的html和javascript代码:
return "\(displayName ?? "" ): \(shortDescription ?? "" )\n\(longDescription ?? "")"
这是我使用IFormFile的控制器
<input type="file" id="myfile" class="required" />
<button type="button" class="btn btn-info" onclick="uploadcsvfile()">
<script>
function uploadcsvfile() {
var myfile= document.getElementById("myfile");
var formData = new FormData();
if (myfile.files.length > 0) {
for (var i = 0; i < myfile.files.length; i++) {
formData.append('file-' + i, myfile.files[i]);
}
}
$.ajax({
url: "/MyController/UploadFile/",
type: "POST",
dataType: "json",
data: formData,
contentType: false,
processData: false,
success: function(data){
},
error: function (data) {
}
})
}
</script>
提前谢谢!
答案 0 :(得分:13)
很好地解释了here:
<form id="form" name="form" action="/uploader" enctype="multipart/form-data" method="post">
<div class="buttons">
<div class="upload-button">
<div class="label">Click me!</div>
<input id="files" name="files" type="file" size="1" multiple onchange="uploadFiles('files');" />
</div>
</div>
</form>
function uploadFiles(inputId) {
var input = document.getElementById(inputId);
var files = input.files;
var formData = new FormData();
for (var i = 0; i != files.length; i++) {
formData.append("files", files[i]);
}
$.ajax(
{
url: "/uploader",
data: formData,
processData: false,
contentType: false,
type: "POST",
success: function (data) {
alert("Files Uploaded!");
}
}
);
}
[HttpPost]
public async Task<IActionResult> Index(IList<IFormFile> files)
{
foreach (IFormFile source in files)
{
string filename = ContentDispositionHeaderValue.Parse(source.ContentDisposition).FileName.Trim('"');
filename = this.EnsureCorrectFilename(filename);
using (FileStream output = System.IO.File.Create(this.GetPathAndFilename(filename)))
await source.CopyToAsync(output);
}
return this.View();
}
private string EnsureCorrectFilename(string filename)
{
if (filename.Contains("\\"))
filename = filename.Substring(filename.LastIndexOf("\\") + 1);
return filename;
}
private string GetPathAndFilename(string filename)
{
return this.hostingEnvironment.WebRootPath + "\\uploads\\" + filename;
}
答案 1 :(得分:10)
这是将文件发布到控制器操作的简单方法。
查看强>:
var formData = new FormData();
formData.append('file', $('#myfile')[0].files[0]); // myFile is the input type="file" control
var _url = '@Url.Action("UploadFile", "MyController")';
$.ajax({
url: _url,
type: 'POST',
data: formData,
processData: false, // tell jQuery not to process the data
contentType: false, // tell jQuery not to set contentType
success: function (result) {
},
error: function (jqXHR) {
},
complete: function (jqXHR, status) {
}
});
<强>控制器强>:
[HttpPost]
public ActionResult UploadFile(IFormFile file)
{
List<string> errors = new List<string>(); // added this just to return something
if (file != null)
{
// do something
}
return Json(errors, JsonRequestBehavior.AllowGet);
}
答案 2 :(得分:2)
您只需指定文件输入“name”属性(与ASP.NET控制器中的变量名相同)。 HTML:
<input type="file" name="thefile" />
C#:
public ActionResult UploadFile(IFormFile thefile) { }
对于AJAX请求,您需要在FormData对象中指定适当的名称。
答案 3 :(得分:0)
完全可行的示例:上述答案中的部分答案已解决了编译错误。
假设您要上传文件,那么您将提交带有已上传文件的表单。
Register.cshtml
@using UploadFileAjaxPostWebApp.Models.Account
@model RegisterModel
@using (Html.BeginForm("Register", "Account", FormMethod.Post))
{
<div>
<label>First Name </label>
<input type="text" name="FirstName" value="John" />
</div>
<div>
<label>Second Name </label>
<input type="text" name="SecondName" value="Smith" />
</div>
<div>
<label>Resume</label>
<input type="file" id="fileUpload1" onchange="uploadFiles('fileUpload1');" />
<input type="hidden" id="ResumeFileName" name="ResumeFileName" value="@Model.ResumeFileName" />
</div>
<div>
<input type="submit" value="Submit" />
</div>
}
<script type="text/javascript">
function uploadFiles(inputId) {
var input = document.getElementById(inputId);
var files = input.files;
var formData = new FormData();
for (var i = 0; i !== files.length; i++) {
formData.append("files", files[i]);
}
$.ajax(
{
url: "/account/uploadfiles",
data: formData,
processData: false,
contentType: false,
type: "POST",
success: function (data) {
// Set the property of the Model.
$("#ResumeFileName").val(data.fileName);
alert("Files Uploaded! " + data.fileName);
}
}
);
}
</script>
帐户控制器:
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using UploadFileAjaxPostWebApp.Models.Account;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;
namespace UploadFileAjaxPostWebApp.Controllers
{
public class AccountController : Controller
{
private readonly IWebHostEnvironment _hostEnvironment;
public AccountController(IWebHostEnvironment hostEnvironment)
{
_hostEnvironment = hostEnvironment;
}
public IActionResult Register()
{
RegisterModel model = new RegisterModel();
return View(model);
}
[HttpPost]
public IActionResult Register(RegisterModel model)
{
return View(model);
}
[HttpPost]
public async Task<ActionResult> UploadFiles(IList<IFormFile> files)
{
string fileName = null;
foreach (IFormFile source in files)
{
// Get original file name to get the extension from it.
string orgFileName = ContentDispositionHeaderValue.Parse(source.ContentDisposition).FileName.Value;
// Create a new file name to avoid existing files on the server with the same names.
fileName = DateTime.Now.ToFileTime() + Path.GetExtension(orgFileName);
string fullPath = GetFullPathOfFile(fileName);
// Create the directory.
Directory.CreateDirectory(Directory.GetParent(fullPath).FullName);
// Save the file to the server.
await using FileStream output = System.IO.File.Create(fullPath);
await source.CopyToAsync(output);
}
var response = new { FileName = fileName };
return Ok(response);
}
private string GetFullPathOfFile(string fileName)
{
return $"{_hostEnvironment.WebRootPath}\\uploads\\{fileName}";
}
}
}
RegisterModel类
namespace UploadFileAjaxPostWebApp.Models.Account
{
public class RegisterModel
{
public string FirstName { get; set; }
public string SecondName { get; set; }
public string ResumeFileName { get; set; }
}
}
答案 4 :(得分:0)
客户端: 而不是设置 file-i add fromData same 作为您的操作方法参数名称
formData.append('formData' myfile.files[i]);
function uploadcsvfile() {
var myfile= document.getElementById("myfile");
var formData = new FormData();
if (myfile.value.toLowerCase().lastIndexOf(".csv") == -1)
{
alert("Please upload a file with .csv extension.");
return false;
}
// else code to upload
}
Html:添加多个属性
<input type="file" id="myfile" class="required" multiple />
ServerSide:添加IFromFile
列表public async Task<JsonResult> UploadFile(List<IFormFile> formData)
{
// do something here
}
如果不使用表单标签,则添加@Html.AntiForgeryToken()
答案 5 :(得分:0)
var GetImages = $('[name="Images"]');
console.log(GetImages[0].files)
for (var i = 0; i != GetImages[0].files.length; i++) {
form.append("Images", GetImages[0].files[i]);
}