我正在尝试将图像与表单一起上传,这在本地主机上运行良好,但在部署到 Microsoft azure 后,我不断收到 500 内部服务器错误。当我注释掉下面的代码后,表单数据已成功保存到数据库中。所以我不明白为什么上传可以在本地工作,但不能在 azure 上工作。
//upload image
var postedFile = httpRequest.Files["Picture"];
if (postedFile != null && postedFile.ContentLength > 0)
{
//create custom filename
imageName = new string(Path.GetFileNameWithoutExtension(postedFile.FileName).Take(10).ToArray()).Replace(" ", "-");
imageName = imageName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(postedFile.FileName);
var filePath = HttpContext.Current.Server.MapPath("~/Upload/" + imageName);
postedFile.SaveAs(filePath);
}
最佳答案
您可能需要先使用 System.IO.Directory.CreateDirectory(path);
创建目录.
检查下面的实现;
var postedFile = httpRequest.Files["Picture"];
if (postedFile != null && postedFile.ContentLength > 0)
{
//create custom filename
imageName = new string(Path.GetFileNameWithoutExtension(postedFile.FileName).Take(10).ToArray()).Replace(" ", "-");
imageName = imageName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(postedFile.FileName);
// create directory
var path = Server.MapPath("~/Upload");
if (!System.IO.Directory.Exists(path))
{
System.IO.Directory.CreateDirectory(path);
}
// then save
var filePath = HttpContext.Current.Server.MapPath("~/Upload/" + imageName);
postedFile.SaveAs(filePath);
}
关于c# - .Net 上传图像 - Microsoft Azure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61144112/