简体   繁体   English

Uri编码,文件下载不适用于,并且空格

[英]Uri encoding, file download doesn't work with , and whitespace

I have an asp.net webpage that allows downloading files. 我有一个允许下载文件的asp.net网页。

When i had the problem of downloading a file with whitespace(Test 1 4 3.txt) it will turn the file into: Test+1+4+3.txt 当我在下载带有whitespace(Test 1 4 3.txt)的文件whitespace(Test 1 4 3.txt)时遇到问题,它将把文件变成: Test+1+4+3.txt

I used: 我用了:

_Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));

And it solved the issue. 它解决了问题。

Now i have a new issue: When a file contains , it changes it into %2c and i the UrlEncode doesn't fix it. 现在,我遇到了一个新问题:文件包含时,它将其更改为%2c ,而UrlEncode无法修复它。 i Tried using: 我尝试使用:

_Response.AddHeader("Content-Disposition", "attachment;filename=" + Uri.EscapeDataString(_fileName));

But it's the old setting and it doesn't support whitespace. 但这是旧设置,不支持空格。

What can i do to solve such a case? 我该怎么办才能解决这种情况? Should i use regex / switch? 我应该使用正则表达式/开关吗?

This is my function: 这是我的功能:

 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;
    }

这解决了它:_Response.AddHeader(“ Content-Disposition”,“ attachment; filename = \\”“ + _fileName +” \\“”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM