gpt4 book ai didi

c# - Azure 不支持外语文件格式名称下载

转载 作者:行者123 更新时间:2023-12-03 04:02:11 25 4
gpt4 key购买 nike

当我试图从基于 azure 的托管网站下载文件时,该网站显示以下内容。
Azure website它的文件上有一个俄语名称,现在当我下载时,它会以某种机器语言显示,如上图所示,因为我正在使用 Chrome 下载该文件。

现在,当我从本地项目下载本地计算机上的图像时,它会为我提供该语言下载的文件,下面是一个示例。
Local machine正如您所看到的,文件名在该语言中是相同的。

客户端对 Azure 上的语言全局化的 Web 配置进行了一些更改,但没有成功。

如果有人有任何解决方案,请建议我。谢谢

最佳答案

更新

我之前的回答是正确的。我现在将向您展示我所有的测试代码。您可以编写一个演示测试来完全解决您的问题。

enter image description here

下面是我的代码:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace Downloadasp.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}

public ActionResult About()
{
ViewBag.Message = "Your application description page.";

return View();
}

public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";

return View();
}
public void DownloadOperation()
{
// Original file name
string fileName = "- room__комната.jpg";
string strFilePath = "/upload/image/20180307/";
// Server Path
string filepath = Server.MapPath(strFilePath)+fileName;
FileInfo fileInfo = new FileInfo(filepath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
// Solve garbled file names
if (Request.UserAgent.Contains("MSIE") || Request.UserAgent.Contains("msie"))
{
// If the client uses Microsoft Internet Explorer, encoding is required
fileName = ToHexString(fileName);
}
// The core part of the problem solving
// Already tested
Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.AddHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "application/octet-stream";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
Response.WriteFile(fileInfo.FullName);
Response.Flush();
Response.End();
}

/// <summary>
/// Encode non-English characters in the string
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string ToHexString(string s)
{
char[] chars = s.ToCharArray();
StringBuilder builder = new StringBuilder();
for (int index = 0; index < chars.Length; index++)
{
bool needToEncode = NeedToEncode(chars[index]);
if (needToEncode)
{
string encodedString = ToHexString(chars[index]);
builder.Append(encodedString);
}
else
{
builder.Append(chars[index]);
}
}

return builder.ToString();
}

/// <summary>
///Specifies whether a character should be encoded
/// </summary>
/// <param name="chr"></param>
/// <returns></returns>
private static bool NeedToEncode(char chr)
{
string reservedChars = "$-_.+!*'(),@=&";

if (chr > 127)
return true;
if (char.IsLetterOrDigit(chr) || reservedChars.IndexOf(chr) >= 0)
return false;

return true;
}

/// <summary>
/// Encode non-English strings
/// </summary>
/// <param name="chr"></param>
/// <returns></returns>
private static string ToHexString(char chr)
{
UTF8Encoding utf8 = new UTF8Encoding();
byte[] encodedBytes = utf8.GetBytes(chr.ToString());
StringBuilder builder = new StringBuilder();
for (int index = 0; index < encodedBytes.Length; index++)
{
builder.AppendFormat("%{0}", Convert.ToString(encodedBytes[index], 16));
}
return builder.ToString();
}


public bool IsReusable
{
get
{
return false;
}
}
}
}

隐私

解决文件名乱码的核心代码语句。

System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8);

示例代码:

System.IO.FileInfo file = new System.IO.FileInfo("filename.xls");
Response.Clear();
// Solve garbled file names
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(file.Name));

Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
Response.Flush();
Response.Clear();

关于c# - Azure 不支持外语文件格式名称下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61747972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com