Skip to content

Commit d9672b6

Browse files
committed
Fixed #30 which caused exception into the log
1 parent 254be02 commit d9672b6

1 file changed

Lines changed: 13 additions & 124 deletions

File tree

Helpers.cs

Lines changed: 13 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -918,13 +918,8 @@ public void DownloadFile(string ItemID)
918918
public void DownloadFile(string ItemID, string target)
919919
{
920920
RepositoryController repository = new RepositoryController();
921-
RepositoryInfo objRepository = null;
922-
int i = 0;
923-
int iExtension = 0;
924-
int iStart = 0;
925-
int iEnd = 0;
921+
RepositoryInfo objRepository = null;
926922
string strDownloadURL = "";
927-
string strURLTarget = "";
928923

929924
objRepository = repository.GetSingleRepositoryObject(Convert.ToInt32(ItemID));
930925

@@ -982,126 +977,20 @@ public void DownloadFile(string ItemID, string target)
982977
}
983978

984979

985-
private void StreamFile(string FilePath, string DownloadAs)
986-
{
987-
DownloadAs = DownloadAs.Replace(" ", "_");
988-
989-
System.IO.FileInfo objFile = new System.IO.FileInfo(FilePath);
990-
System.Web.HttpResponse objResponse = System.Web.HttpContext.Current.Response;
991-
objResponse.ClearContent();
992-
993-
objResponse.ClearHeaders();
994-
objResponse.AppendHeader("Content-Disposition", "attachment; filename=" + DownloadAs);
995-
objResponse.AppendHeader("Content-Length", objFile.Length.ToString());
996-
997-
string strContentType = null;
998-
switch (objFile.Extension) {
999-
case ".txt":
1000-
strContentType = "text/plain";
1001-
break;
1002-
case ".htm":
1003-
case ".html":
1004-
strContentType = "text/html";
1005-
break;
1006-
case ".rtf":
1007-
strContentType = "text/richtext";
1008-
break;
1009-
case ".jpg":
1010-
case ".jpeg":
1011-
strContentType = "image/jpeg";
1012-
break;
1013-
case ".gif":
1014-
strContentType = "image/gif";
1015-
break;
1016-
case ".bmp":
1017-
strContentType = "image/bmp";
1018-
break;
1019-
case ".mpg":
1020-
case ".mpeg":
1021-
strContentType = "video/mpeg";
1022-
break;
1023-
case ".avi":
1024-
strContentType = "video/avi";
1025-
break;
1026-
case ".pdf":
1027-
strContentType = "application/pdf";
1028-
break;
1029-
case ".doc":
1030-
case ".dot":
1031-
strContentType = "application/msword";
1032-
break;
1033-
case ".csv":
1034-
case ".xls":
1035-
case ".xlt":
1036-
strContentType = "application/vnd.msexcel";
1037-
break;
1038-
default:
1039-
strContentType = "application/octet-stream";
1040-
break;
1041-
}
1042-
objResponse.ContentType = strContentType;
1043-
WriteFile(objFile.FullName);
980+
private void StreamFile(string FilePath, string DownloadAs)
981+
{
982+
DownloadAs = DownloadAs.Replace(" ", "_");
1044983

1045-
objResponse.Flush();
1046-
objResponse.Close();
1047-
1048-
}
1049-
1050-
public static void WriteFile(string strFileName)
1051-
{
1052-
System.Web.HttpResponse objResponse = System.Web.HttpContext.Current.Response;
1053-
System.IO.Stream objStream = null;
1054-
1055-
// Buffer to read 10K bytes in chunk:
1056-
byte[] bytBuffer = new byte[10001];
1057-
1058-
// Length of the file:
1059-
int intLength = 0;
1060-
1061-
// Total bytes to read:
1062-
long lngDataToRead = 0;
1063-
1064-
try {
1065-
// Open the file.
1066-
objStream = new System.IO.FileStream(strFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
984+
System.IO.FileInfo objFile = new System.IO.FileInfo(FilePath);
985+
System.Web.HttpResponse objResponse = System.Web.HttpContext.Current.Response;
986+
objResponse.ClearContent();
987+
objResponse.ClearHeaders();
1067988

1068-
// Total bytes to read:
1069-
lngDataToRead = objStream.Length;
1070-
1071-
objResponse.ContentType = "application/octet-stream";
1072-
1073-
// Read the bytes.
1074-
while (lngDataToRead > 0) {
1075-
// Verify that the client is connected.
1076-
if (objResponse.IsClientConnected) {
1077-
// Read the data in buffer
1078-
intLength = objStream.Read(bytBuffer, 0, 10000);
1079-
1080-
// Write the data to the current output stream.
1081-
objResponse.OutputStream.Write(bytBuffer, 0, intLength);
1082-
1083-
// Flush the data to the HTML output.
1084-
objResponse.Flush();
1085-
1086-
bytBuffer = new byte[10001];
1087-
// Clear the buffer
1088-
lngDataToRead = lngDataToRead - intLength;
1089-
} else {
1090-
//prevent infinite loop if user disconnects
1091-
lngDataToRead = -1;
1092-
}
1093-
}
1094-
1095-
} catch (Exception ex) {
1096-
// Trap the error, if any.
1097-
// objResponse.Write("Error : " & ex.Message)
1098-
} finally {
1099-
if ((objStream == null) == false) {
1100-
// Close the file.
1101-
objStream.Close();
1102-
}
1103-
}
1104-
}
989+
objResponse.AppendHeader("Content-Type", MimeMapping.GetMimeMapping(objFile.Extension));
990+
objResponse.AppendHeader("Content-Disposition", "attachment; filename=" + DownloadAs);
991+
objResponse.AppendHeader("Content-Length", objFile.Length.ToString());
992+
objResponse.TransmitFile(objFile.FullName);
993+
}
1105994

1106995
#endregion
1107996

0 commit comments

Comments
 (0)