c# - 如何在 Microsoft Azure 虚拟服务器上创建和保存临时文件

标签 c# azure

我正在为我的网站使用免费 MS Azure 虚拟网络服务器。

在我的开发计算机上,我可以成功创建 CSV 文件,将其保存到相对临时目录,然后将其下载到浏览器客户端。

但是,当我从 Azure 站点运行它时,出现以下错误:

System.IO.DirectoryNotFoundException: Could not find a part of the path 'D:\home\site\wwwroot\temp\somefile.csv'.

Azure 网站的免费版本是否会阻止我们将文件保存到磁盘?如果没有,我们可以在哪里创建/保存动态生成的文件?

代码示例

private FilePathResult SaveVolunteersToCsvFile(List<Volunteer> volunteers)
{
    string virtualPathToDirectory = "~/temp";
    string physicalPathToDirectory = Server.MapPath(virtualPathToDirectory);
    string fileName = "Volunteers.csv";
    string pathToFile = Path.Combine(physicalPathToDirectory, fileName);
    StringBuilder sb = new StringBuilder();

    // Column Headers
    sb.AppendLine("First Name,Last Name,Phone,Email,Approved,Has Background Check");

    // CSV Rows
    foreach (var volunteer in volunteers)
    {
        sb.AppendLine(string.Format("{0},{1},{2},{3},{4},{5},{6}",
            volunteer.FirstName, volunteer.LastName, volunteer.MobilePhone.FormatPhoneNumber(), volunteer.EmailAddress, volunteer.IsApproved, volunteer.HasBackgroundCheckOnFile));
    }

    using (StreamWriter outfile = new StreamWriter(pathToFile))
    {
        outfile.Write(sb.ToString());
    }
    return File(Server.MapPath(virtualPathToDirectory + "/" + fileName), "text/csv", fileName);
}

最佳答案

确保 ~/temp 文件夹发布到服务器,因为您的发布过程可能不包含它。

关于c# - 如何在 Microsoft Azure 虚拟服务器上创建和保存临时文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25391244/

相关文章:

c# - mysql 选择像列一样的列1 或列2

azure - 如何在 Azure 中使用 ARM 模板打印链接模板的输出

python - 在 Azure ML 中保存 torch 模型

azure - VSTS 扩展 : "Cannot process command because of one or more missing mandatory parameters: appdirectory webappname ResourceGroupName"

azure - Web 应用程序下 Azure 中允许的最大 webJobs

c# - Azure 服务证书限制

c# - 使用 Style 将 Calendar 的宽度更改为其父 DatePicker 的宽度

c# - 检查范围和设置默认值时简化 if 子句

c# - 在 C# 中将 Stream 转换为 FileStream

c# - 在 asp.net ascx 中使用变量 DataFormatString