asp.net core 跨平台文件上传
[删除(380066935@qq.com或微信通知)]
if (fileimg != null && string.IsNullOrEmpty(model.Img))
{
if (fileimg.Length > 0)
{
string dirPath = webEnvironment.WebRootPath + Path.DirectorySeparatorChar + "upload" + Path.DirectorySeparatorChar + "slider";
string uploadpathUrl = "/upload/slider";
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
{
string fileExt = Path.GetExtension(fileimg.FileName).ToLower();
string newFileName = Guid.NewGuid().ToString("N") + fileExt;
using (var stream = new FileStream(dirPath + "/" + newFileName, FileMode.Create))
{
fileimg.CopyTo(stream);
model.Img = (uploadpathUrl + "/" + newFileName.Trim('"'));
}
}
}
}