前端实现在如下
@inherits ProductionOrderComponentBase
@if (IsValidating)
{
}
else
{
if (ComponentMode == ComponentMode.List)
{
@if (ReadOnly == false)
{
}
@* *@
@{
var requestNumb = $"{((ProductionOrderWithDetail) context).OrderNumber:000000}";
@requestNumb
}
}
else
{
@if (CanChange)
{
@productContext.Code
@productContext.Code
}
else
{
}
* Auto-generate after saved.
}
}
@code {
protected string GetUploadUrl(string url)
{
return NavigationManager.ToAbsoluteUri(url).AbsoluteUri;
}
}
API控制器实现
using DevExpress.Blazor.Upload.Internal;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
namespace Client.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class FilesController : ControllerBase
{
private static readonly long FileSize = 10485760;
private static readonly string FilesType = ".gif,.jpg,.jpeg,.png,.bmp,.GIF,.JPG,.JPEG,.PNG,.doc,.docx,.xls,.xlsx,.csv,.pdf,.ppt,.dwg,.DXF,.zip,.rar,.txt";
private static readonly string FilesBasePath = "c:\\wms_file";
//在IIS新建图片服务器网站,路径指向 FilesBasePath
private static readonly string Ip = "10.111.11.123";
private static readonly string Port = "8899";
void RemoveTempFilesAfterDelay(string path, TimeSpan delay)
{
var dir = new DirectoryInfo(path);
if (dir.Exists)
foreach (var file in dir.GetFiles("*.tmp").Where(f => f.LastWriteTimeUtc.Add(delay) < DateTime.UtcNow))
file.Delete();
}
void ProcessUploadedFile(string tempFilePath, string fileName)
{
var path = Path.Combine(FilesBasePath, "Images");
var imagePath = Path.Combine(path, fileName);
if (System.IO.File.Exists(imagePath))
System.IO.File.Delete(imagePath);
System.IO.File.Copy(tempFilePath, imagePath);
}
void AppendContentToFile(string path, IFormFile content)
{
using (var stream = new FileStream(path, FileMode.Append, FileAccess.Write))
{
content.CopyTo(stream);
}
}
public class ChunkMetadata
{
public int Index { get; set; }
public int TotalCount { get; set; }
public int FileSize { get; set; }
public string FileName { get; set; }
public string FileType { get; set; }
public Guid FileGuid { get; set; }
}
[HttpPost]
[Route("UploadFile")]
[DisableRequestSizeLimit]
public ActionResult UploadFile(IFormFile myFile)
{
string chunkMetadata = Request.Form["chunkMetadata"];
var tempPath = Path.Combine(FilesBasePath, "Images");
// Removes temporary files
RemoveTempFilesAfterDelay(tempPath, new TimeSpan(0, 5, 0));
try
{
if (!string.IsNullOrEmpty(chunkMetadata))
{
var metaDataObject = JsonConvert.DeserializeObject
var tempFilePath = Path.Combine(tempPath, metaDataObject.FileGuid + ".tmp");
if (!Directory.Exists(tempPath))
Directory.CreateDirectory(tempPath);
AppendContentToFile(tempFilePath, myFile);
if (metaDataObject.Index == (metaDataObject.TotalCount - 1))
{
ProcessUploadedFile(tempFilePath, metaDataObject.FileName);
}
}
}
catch (Exception ex)
{
return BadRequest();
}
return Ok();
}
///
/// 上传文件
///
[HttpPost("Upload")]
public ActionResult Upload(IFormFile myFile)
{
try
{
var id = "WMS";//我自己的combine id生成器
string newFileName = string.Empty;
if (myFile == null)
{
throw new Exception("传入文件为空");//类型
}
if (myFile.Length <= FileSize)//检查文件大小
{
var suffix = Path.GetExtension(myFile.FileName);//提取上传的文件文件后缀
if (FilesType.IndexOf(suffix) != -1)//检查文件格式
{
newFileName = myFile.FileName + suffix;
//创建每日存储文件夹
if (Directory.Exists($@"{FilesBasePath}\{id}"))
{
//删除原有文件防止文件名重复
DirectoryInfo subdir = new DirectoryInfo($@"{FilesBasePath}\{id}");
subdir.Delete(true);
}
Directory.CreateDirectory($@"{FilesBasePath}\{id}");
using (FileStream fs = System.IO.File.Create($@"{FilesBasePath}\{id}\{newFileName}"))//注意路径里面最好不要有中文
{
myFile.CopyTo(fs);//将上传的文件文件流,复制到fs中
fs.Flush();//清空文件流
}
}
else
throw new Exception("不支持此文件类型");//类型不正确
}
else
{
throw new Exception($"文件大小不得超过{FileSize / (1024f * 1024f)}M"); //请求体过大,文件大小超标
}
return Ok(new { code = 0 });
}
catch (Exception ex)
{
return Ok(new { code = ex.Message });
}
}
}
}