c# - .Net 上传图像 - Microsoft Azure

标签 c# asp.net-mvc azure azure-web-app-service

我正在尝试将图像与表单一起上传,这在本地主机上运行良好,但在部署到 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/

相关文章:

c# - 继承类并调用隐藏的基类构造函数

c# - 在 XSD 中表示一对重复的 XML 元素

c# - 自定义数据注释 ASP.NET MVC C#

azure - ACI - Windows Server 2019 - 无法连接到互联网

azure - 将数据从外部源放入 Azure Blob 存储

c# - 如何使用键将 DataTextField 绑定(bind)到字典值

c# - "asp-format"不适用于标签助手

c# - 一个 View 中的 MVC 多个模型

azure - 为什么我的 Azure Function 找不到 Microsoft.Crm.Sdk.Proxy 程序集依赖项?

c# - 使用 C++ 在 C# 位图上绘制