电脑故障

位置:IT落伍者 >> 电脑故障 >> 浏览文章

文件下载程序中文件名过长的问题


发布日期:2023/6/21
 

今天测试文件下载程序中发现的文件名过长的问题 居然发现文件名编码后长度超过就会不能正确显示和下载最后只好找了这样一个折中的方法截短了

下面是那里的代码

/// <summary>

/// 下载附件

/// </summary>

/// <param name=fileName>文件名</param>

/// <param name=path>文件路径</param>

public static void DownLoadFileAttachment(string fileName string path)

{

if (SystemIOFileExists(path))

{

try

{

fileName= fileNameTrim();

for (inti = ; i < SystemIOPathInvalidPathCharsLength ; i ++)

{

fileName = fileNameTrim()Replace(SystemIOPathInvalidPathChars[i]ToString() stringEmpty);

}

fileName= fileNameReplace(SystemIOPathPathSeparatorToString() stringEmpty);

int maxLength = ;

int length= HttpUtilityUrlEncode(fileName)Length;

while (length > maxLength)

{

int index = fileNameLastIndexOf();

if (index > )

{

fileName = fileNameSubstring( index ) + fileNameSubstring(index);

}

else

{

fileName = fileNameSubstring( fileNameLength );

}

length= HttpUtilityUrlEncode(fileName)Length;

}

SystemIOFileInfo file = new SystemIOFileInfo(path);

HttpContextCurrentResponseClear();

HttpContextCurrentResponseAppendHeader(ContentDisposition attachment; filename= + HttpUtilityUrlEncode(fileName));

HttpContextCurrentResponseAppendHeader(ContentLength fileLengthToString());

HttpContextCurrentResponseContentType = application/octetstream;

HttpContextCurrentResponseWriteFile(fileFullName);

HttpContextCurrentResponseEnd();

}

catch

{

}

}

else

{

HttpContextCurrentResponseClear();

DisplayNoFileMessage();

HttpContextCurrentResponseEnd();

}

}

上一篇:JBuilder 用户 NetBeans IDE 指南

下一篇:NetBeans 6.9增强了互操作性