(前言:这个问题是关于2011年发布的ASP.NET MVC 3.0,而不是关于2019年发布的ASP.NETCore 3.0)
我想用asp.net mvc上传文件。如何使用html输入文件控件上载文件?
(前言:这个问题是关于2011年发布的ASP.NET MVC 3.0,而不是关于2019年发布的ASP.NETCore 3.0)
我想用asp.net mvc上传文件。如何使用html输入文件控件上载文件?
当前回答
我给你简单易懂的方法。
首先,必须在.Cshtml文件中编写以下代码。
<input name="Image" type="file" class="form-control" id="resume" />
然后在控制器中输入以下代码:
if (i > 0) {
HttpPostedFileBase file = Request.Files["Image"];
if (file != null && file.ContentLength > 0) {
if (!string.IsNullOrEmpty(file.FileName)) {
string extension = Path.GetExtension(file.FileName);
switch ((extension.ToLower())) {
case ".doc":
break;
case ".docx":
break;
case ".pdf":
break;
default:
ViewBag.result = "Please attach file with extension .doc , .docx , .pdf";
return View();
}
if (!Directory.Exists(Server.MapPath("~") + "\\Resume\\")) {
System.IO.Directory.CreateDirectory(Server.MapPath("~") + "\\Resume\\");
}
string documentpath = Server.MapPath("~") + "\\Resume\\" + i + "_" + file.FileName;
file.SaveAs(documentpath);
string filename = i + "_" + file.FileName;
result = _objbalResume.UpdateResume(filename, i);
Attachment at = new Attachment(documentpath);
//ViewBag.result = (ans == true ? "Thanks for contacting us.We will reply as soon as possible" : "There is some problem. Please try again later.");
}
} else {
...
}
}
为此,您必须根据您的数据库制作BAL和DAL层。
其他回答
我必须上传100kb的文件块,最后一个上传文件使用命令存储在数据库中。我希望,这会对你有所帮助。
public HttpResponseMessage Post(AttachmentUploadForm form)
{
var response = new WebApiResultResponse
{
IsSuccess = true,
RedirectRequired = false
};
var tempFilesFolder = Sanelib.Common.SystemSettings.Globals.CreateOrGetCustomPath("Temp\\" + form.FileId);
File.WriteAllText(tempFilesFolder + "\\" + form.ChunkNumber + ".temp", form.ChunkData);
if (form.ChunkNumber < Math.Ceiling((double)form.Size / 102400)) return Content(response);
var folderInfo = new DirectoryInfo(tempFilesFolder);
var totalFiles = folderInfo.GetFiles().Length;
var sb = new StringBuilder();
for (var i = 1; i <= totalFiles; i++)
{
sb.Append(File.ReadAllText(tempFilesFolder + "\\" + i + ".temp"));
}
var base64 = sb.ToString();
base64 = base64.Substring(base64.IndexOf(',') + 1);
var fileBytes = Convert.FromBase64String(base64);
var fileStream = new FileStream(tempFilesFolder + "\\" + form.Name, FileMode.OpenOrCreate, FileAccess.ReadWrite);
fileStream.Seek(fileStream.Length, SeekOrigin.Begin);
fileStream.Write(fileBytes, 0, fileBytes.Length);
fileStream.Close();
Directory.Delete(tempFilesFolder, true);
var md5 = MD5.Create();
var command = Mapper.Map<AttachmentUploadForm, AddAttachment>(form);
command.FileData = fileBytes;
command.FileHashCode = BitConverter.ToString(md5.ComputeHash(fileBytes)).Replace("-", "");
return ExecuteCommand(command);
}
Javascript(Knockout Js)
define(['util', 'ajax'], function (util, ajax) {
"use strict";
var exports = {},
ViewModel, Attachment, FileObject;
//File Upload
FileObject = function (file, parent) {
var self = this;
self.fileId = util.guid();
self.name = ko.observable(file.name);
self.type = ko.observable(file.type);
self.size = ko.observable();
self.fileData = null;
self.fileSize = ko.observable(file.size / 1024 / 1024);
self.chunks = 0;
self.currentChunk = ko.observable();
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function (e) {
self.fileData = e.target.result;
self.size(self.fileData.length);
self.chunks = Math.ceil(self.size() / 102400);
self.sendChunk(1);
});
reader.readAsDataURL(file);
self.percentComplete = ko.computed(function () {
return self.currentChunk() * 100 / self.chunks;
}, self);
self.cancel = function (record) {
parent.uploads.remove(record);
};
self.sendChunk = function (number) {
var start = (number - 1) * 102400;
var end = number * 102400;
self.currentChunk(number);
var form = {
fileId: self.fileId,
name: self.name(),
fileType: self.type(),
Size: self.size(),
FileSize: self.fileSize(),
chunkNumber: number,
chunkData: self.fileData.slice(start, end),
entityTypeValue: parent.entityTypeValue,
ReferenceId: parent.detail.id,
ReferenceName: parent.detail.name
};
ajax.post('Attachment', JSON.stringify(form)).done(function (response) {
if (number < self.chunks)
self.sendChunk(number + 1);
if (response.id != null) {
parent.attachments.push(new Attachment(response));
self.cancel(response);
}
});
};
};
Attachment = function (data) {
var self = this;
self.id = ko.observable(data.id);
self.name = ko.observable(data.name);
self.fileType = ko.observable(data.fileType);
self.fileSize = ko.observable(data.fileSize);
self.fileData = ko.observable(data.fileData);
self.typeName = ko.observable(data.typeName);
self.description = ko.observable(data.description).revertable();
self.tags = ko.observable(data.tags).revertable();
self.operationTime = ko.observable(moment(data.createdOn).format('MM-DD-YYYY HH:mm:ss'));
self.description.subscribe(function () {
var form = {
Id: self.id(),
Description: self.description(),
Tags: self.tags()
};
ajax.put('attachment', JSON.stringify(form)).done(function (response) {
self.description.commit();
return;
}).fail(function () {
self.description.revert();
});
});
self.tags.subscribe(function () {
var form = {
Id: self.id(),
Description: self.description(),
Tags: self.tags()
};
ajax.put('attachment', JSON.stringify(form)).done(function (response) {
self.tags.commit();
return;
}).fail(function () {
self.tags.revert();
});
});
};
ViewModel = function (data) {
var self = this;
// for attachment
self.attachments = ko.observableArray([]);
$.each(data.attachments, function (row, val) {
self.attachments.push(new Attachment(val));
});
self.deleteAttachmentRecord = function (record) {
if (!confirm("Are you sure you want to delete this record?")) return;
ajax.del('attachment', record.id(), { async: false }).done(function () {
self.attachments.remove(record);
return;
});
};
exports.exec = function (model) {
console.log(model);
var viewModel = new ViewModel(model);
ko.applyBindings(viewModel, document.getElementById('ShowAuditDiv'));
};
return exports;
});
HTML代码:
<div class="row-fluid spacer-bottom fileDragHolder">
<div class="spacer-bottom"></div>
<div class="legend">
Attachments<div class="pull-right">@Html.AttachmentPicker("AC")</div>
</div>
<div>
<div class="row-fluid spacer-bottom">
<div style="overflow: auto">
<table class="table table-bordered table-hover table-condensed" data-bind="visible: uploads().length > 0 || attachments().length > 0">
<thead>
<tr>
<th class=" btn btn-primary col-md-2" style="text-align: center">
Name
</th>
<th class="btn btn-primary col-md-1" style="text-align: center">Type</th>
<th class="btn btn-primary col-md-1" style="text-align: center">Size (MB)</th>
<th class="btn btn-primary col-md-1" style="text-align: center">Upload Time</th>
<th class="btn btn-primary col-md-1" style="text-align: center">Tags</th>
<th class="btn btn-primary col-md-6" style="text-align: center">Description</th>
<th class="btn btn-primary col-md-1" style="text-align: center">Delete</th>
</tr>
</thead>
<tbody>
<!-- ko foreach: attachments -->
<tr>
<td style="text-align: center" class="col-xs-2"><a href="#" data-bind="text: name,attr:{'href':'/attachment/index?id=' + id()}"></a></td>
<td style="text-align: center" class="col-xs-1"><span data-bind="text: fileType"></span></td>
<td style="text-align: center" class="col-xs-1"><span data-bind="text: fileSize"></span></td>
<td style="text-align: center" class="col-xs-2"><span data-bind="text: operationTime"></span></td>
<td style="text-align: center" class="col-xs-3"><div contenteditable="true" data-bind="editableText: tags"></div></td>
<td style="text-align: center" class="col-xs-4"><div contenteditable="true" data-bind="editableText: description"></div></td>
<td style="text-align: center" class="col-xs-1"><button class="btn btn-primary" data-bind="click:$root.deleteAttachmentRecord"><i class="icon-trash"></i></button></td>
</tr>
<!-- /ko -->
</tbody>
<tfoot data-bind="visible: uploads().length > 0">
<tr>
<th colspan="6">Files upload status</th>
</tr>
<tr>
<th>Name</th>
<th>Type</th>
<th>Size (MB)</th>
<th colspan="2">Status</th>
<th></th>
</tr>
<!-- ko foreach: uploads -->
<tr>
<td><span data-bind="text: name"></span></td>
<td><span data-bind="text: type"></span></td>
<td><span data-bind="text: fileSize"></span></td>
<td colspan="2">
<div class="progress">
<div class="progress-bar" data-bind="style: { width: percentComplete() + '%' }"></div>
</div>
</td>
<td style="text-align: center"><button class="btn btn-primary" data-bind="click:cancel"><i class="icon-trash"></i></button></td>
</tr>
<!-- /ko -->
</tfoot>
</table>
</div>
<div data-bind="visible: attachments().length == 0" class="span12" style="margin-left:0">
<span>No Records found.</span>
</div>
</div>
在视图中:
<form action="Categories/Upload" enctype="multipart/form-data" method="post">
<input type="file" name="Image">
<input type="submit" value="Save">
</form>
而控制器中的以下代码:
public ActionResult Upload()
{
foreach (string file in Request.Files)
{
var hpf = this.Request.Files[file];
if (hpf.ContentLength == 0)
{
continue;
}
string savedFileName = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory, "PutYourUploadDirectoryHere");
savedFileName = Path.Combine(savedFileName, Path.GetFileName(hpf.FileName));
hpf.SaveAs(savedFileName);
}
...
}
保存多个文件的简单方法
cshtml
@using (Html.BeginForm("Index","Home",FormMethod.Post,new { enctype = "multipart/form-data" }))
{
<label for="file">Upload Files:</label>
<input type="file" multiple name="files" id="files" /><br><br>
<input type="submit" value="Upload Files" />
<br><br>
@ViewBag.Message
}
控制器
[HttpPost]
public ActionResult Index(HttpPostedFileBase[] files)
{
foreach (HttpPostedFileBase file in files)
{
if (file != null && file.ContentLength > 0)
try
{
string path = Path.Combine(Server.MapPath("~/Files"), Path.GetFileName(file.FileName));
file.SaveAs(path);
ViewBag.Message = "File uploaded successfully";
}
catch (Exception ex)
{
ViewBag.Message = "ERROR:" + ex.Message.ToString();
}
else
{
ViewBag.Message = "You have not specified a file.";
}
}
return View();
}
检查我的解决方案
public string SaveFile(HttpPostedFileBase uploadfile, string saveInDirectory="/", List<string> acceptedExtention =null)
{
acceptedExtention = acceptedExtention ?? new List<String>() {".png", ".Jpeg"};//optional arguments
var extension = Path.GetExtension(uploadfile.FileName).ToLower();
if (!acceptedExtention.Contains(extension))
{
throw new UserFriendlyException("Unsupported File type");
}
var tempPath = GenerateDocumentPath(uploadfile.FileName, saveInDirectory);
FileHelper.DeleteIfExists(tempPath);
uploadfile.SaveAs(tempPath);
var fileName = Path.GetFileName(tempPath);
return fileName;
}
private string GenerateDocumentPath(string fileName, string saveInDirectory)
{
System.IO.Directory.CreateDirectory(Server.MapPath($"~/{saveInDirectory}"));
return Path.Combine(Server.MapPath($"~/{saveInDirectory}"), Path.GetFileNameWithoutExtension(fileName) +"_"+ DateTime.Now.Ticks + Path.GetExtension(fileName));
}
在基本控制器中添加这些函数,以便可以在所有控制器中使用它们
检查如何使用它
SaveFile(view.PassportPicture,acceptedExtention:new List<String>() { ".png", ".Jpeg"},saveInDirectory: "content/img/PassportPicture");
下面是一个完整的例子
[HttpPost]
public async Task<JsonResult> CreateUserThenGenerateToken(CreateUserViewModel view)
{// CreateUserViewModel contain two properties of type HttpPostedFileBase
string passportPicture = null, profilePicture = null;
if (view.PassportPicture != null)
{
passportPicture = SaveFile(view.PassportPicture,acceptedExtention:new List<String>() { ".png", ".Jpeg"},saveInDirectory: "content/img/PassportPicture");
}
if (view.ProfilePicture != null)
{
profilePicture = SaveFile(yourHttpPostedFileBase, acceptedExtention: new List<String>() { ".png", ".Jpeg" }, saveInDirectory: "content/img/ProfilePicture");
}
var input = view.MapTo<CreateUserInput>();
input.PassportPicture = passportPicture;
input.ProfilePicture = profilePicture;
var getUserOutput = await _userAppService.CreateUserThenGenerateToken(input);
return new AbpJsonResult(getUserOutput);
//return Json(new AjaxResponse() { Result = getUserOutput, Success = true });
}
另一种传输到字节[]的方法(用于保存到DB)。
@Arthur的方法工作得很好,但不能完美复制,因此MS Office文档在从数据库检索后可能无法打开。MemoryStream.GetBuffer()可以在字节[]的末尾返回额外的空字节,但可以使用MemoryStream.ToArray()来解决这个问题。然而,我发现这个替代方案可以完美地适用于所有文件类型:
using (var binaryReader = new BinaryReader(file.InputStream))
{
byte[] array = binaryReader.ReadBytes(file.ContentLength);
}
这是我的完整代码:
文档类别:
public class Document
{
public int? DocumentID { get; set; }
public string FileName { get; set; }
public byte[] Data { get; set; }
public string ContentType { get; set; }
public int? ContentLength { get; set; }
public Document()
{
DocumentID = 0;
FileName = "New File";
Data = new byte[] { };
ContentType = "";
ContentLength = 0;
}
}
文件下载:
[HttpGet]
public ActionResult GetDocument(int? documentID)
{
// Get document from database
var doc = dataLayer.GetDocument(documentID);
// Convert to ContentDisposition
var cd = new System.Net.Mime.ContentDisposition
{
FileName = doc.FileName,
// Prompt the user for downloading; set to true if you want
// the browser to try to show the file 'inline' (display in-browser
// without prompting to download file). Set to false if you
// want to always prompt them to download the file.
Inline = true,
};
Response.AppendHeader("Content-Disposition", cd.ToString());
// View document
return File(doc.Data, doc.ContentType);
}
文件上载:
[HttpPost]
public ActionResult GetDocument(HttpPostedFileBase file)
{
// Verify that the user selected a file
if (file != null && file.ContentLength > 0)
{
// Get file info
var fileName = Path.GetFileName(file.FileName);
var contentLength = file.ContentLength;
var contentType = file.ContentType;
// Get file data
byte[] data = new byte[] { };
using (var binaryReader = new BinaryReader(file.InputStream))
{
data = binaryReader.ReadBytes(file.ContentLength);
}
// Save to database
Document doc = new Document()
{
FileName = fileName,
Data = data,
ContentType = contentType,
ContentLength = contentLength,
};
dataLayer.SaveDocument(doc);
// Show success ...
return RedirectToAction("Index");
}
else
{
// Show error ...
return View("Foo");
}
}
视图(代码段):
@using (Html.BeginForm("GetDocument", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<input type="submit" value="Upload File" />
}