今天测试文件下载程序中发现的文件名过长的问题 居然发现文件名编码后长度超过就会不能正确显示和下载最后只好找了这样一个折中的方法截短了 下面是那里的代码 /// <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(); } } |