我有一个允许下载文件的asp.net网页。
当我遇到使用whitespace(Test 1 4 3.txt)
下载文件的问题时,它会将文件转换为:Test+1+4+3.txt
我用过:
_Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));
它解决了这个问题。
现在我有一个新问题:
当文件包含,
时,它会将其更改为%2c
,并且我的UrlEncode无法修复它。
我尝试使用:
_Response.AddHeader("Content-Disposition", "attachment;filename=" + Uri.EscapeDataString(_fileName));
但它是旧设置,它不支持空白。
我该怎么办才能解决这种情况? 我应该使用正则表达式/开关吗?
这是我的功能:
public static bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed = 1024000)
{
try
{
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
try
{
_Response.AddHeader("Accept-Ranges", "bytes");
_Response.Buffer = false;
long fileLength = myFile.Length;
long startBytes = 0;
int pack = 10240; //10K bytes
int sleep = (int)Math.Floor((double)(1000 * pack / _speed)) + 1;
if (_Request.Headers["Range"] != null)
{
_Response.StatusCode = 206;
string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' });
startBytes = Convert.ToInt64(range[1]);
}
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != 0)
{
_Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
}
_Response.AddHeader("Connection", "Keep-Alive");
_Response.ContentType = "application/octet-stream";
//Old didn't work with both + and ,
//_Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));
_Response.AddHeader("Content-Disposition", "attachment;filename=" + Uri.EscapeDataString(_fileName));
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int)Math.Floor((double)((fileLength - startBytes) / pack)) + 1;
for (int i = 0; i < maxCount; i++)
{
if (_Response.IsClientConnected)
{
_Response.BinaryWrite(br.ReadBytes(pack));
Thread.Sleep(sleep);
}
else
{
i = maxCount;
}
}
}
catch
{
return false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch
{
return false;
}
return true;
}
答案 0 :(得分:0)
这解决了它: _Response.AddHeader(&#34; Content-Disposition&#34;,&#34; attachment; filename = \&#34;&#34; + _fileName +&#34; \&#34;&#34;);