问题描述
我正在.net核心中开发一个mvc项目。 文件上传在本地上运行。 但是当我上线项目时文件上传不起作用。 交易下降到404。
那是我的代码;
public IActionResult Add()
{
var mycategories = ctx.tbCategories.ToList();
SetUserInfo();
return View(mycategories);
}
[HttpPost]
public IActionResult Add(Photos photos,IFormFile imagePath,int[] Categories)
{
string newImageName = photos.name.Replace(' ','-');
if (!string.IsNullOrEmpty(imagePath.ToString()))
{
newImageName = newImageName + Path.GetExtension(imagePath.FileName);
var uploadFolder = Path.Combine(_HostEnvironment.ContentRootPath,"wwwroot/img/photos");
string filePath = Path.Combine(uploadFolder,newImageName);
using (FileStream fs = System.IO.File.Create(filePath))
{
imagePath.copyTo(fs);
fs.Flush();
}
}
photos.createdAt = DateTime.Now;
photos.updatedAt = DateTime.Now;
photos.imagePath = newImageName;
ctx.tbPhotos.Add(photos);
ctx.SaveChanges();
int lastIdOfSavedPhoto = ctx.tbPhotos.Max(u => u.Id);
for (int i = 0; i < Categories.Length; i++)
{
PhotoCategories myPhotoCategories = new PhotoCategories();
myPhotoCategories.photoId = lastIdOfSavedPhoto;
myPhotoCategories.categoryId = int.Parse(Categories[i].ToString());
myPhotoCategories.createdAt = DateTime.Now;
myPhotoCategories.updatedAt = myPhotoCategories.createdAt;
ctx.tbPhotoCategories.Add(myPhotoCategories);
ctx.SaveChanges();
}
return RedirectToAction("Photos","Admin");
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)