Skip to content

Commit 9d424ef

Browse files
authored
Merge pull request #31 from juvander/FixFor30
Improves and simplifies code for download logic
2 parents 03f2e69 + d9672b6 commit 9d424ef

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
@@ -904,13 +904,8 @@ public void DownloadFile(string ItemID)
904904
public void DownloadFile(string ItemID, string target)
905905
{
906906
RepositoryController repository = new RepositoryController();
907-
RepositoryInfo objRepository = null;
908-
int i = 0;
909-
int iExtension = 0;
910-
int iStart = 0;
911-
int iEnd = 0;
907+
RepositoryInfo objRepository = null;
912908
string strDownloadURL = "";
913-
string strURLTarget = "";
914909

915910
objRepository = repository.GetSingleRepositoryObject(Convert.ToInt32(ItemID));
916911

@@ -968,126 +963,20 @@ public void DownloadFile(string ItemID, string target)
968963
}
969964

970965

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

1031-
objResponse.Flush();
1032-
objResponse.Close();
1033-
1034-
}
1035-
1036-
public static void WriteFile(string strFileName)
1037-
{
1038-
System.Web.HttpResponse objResponse = System.Web.HttpContext.Current.Response;
1039-
System.IO.Stream objStream = null;
1040-
1041-
// Buffer to read 10K bytes in chunk:
1042-
byte[] bytBuffer = new byte[10001];
1043-
1044-
// Length of the file:
1045-
int intLength = 0;
1046-
1047-
// Total bytes to read:
1048-
long lngDataToRead = 0;
1049-
1050-
try {
1051-
// Open the file.
1052-
objStream = new System.IO.FileStream(strFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
970+
System.IO.FileInfo objFile = new System.IO.FileInfo(FilePath);
971+
System.Web.HttpResponse objResponse = System.Web.HttpContext.Current.Response;
972+
objResponse.ClearContent();
973+
objResponse.ClearHeaders();
1053974

1054-
// Total bytes to read:
1055-
lngDataToRead = objStream.Length;
1056-
1057-
objResponse.ContentType = "application/octet-stream";
1058-
1059-
// Read the bytes.
1060-
while (lngDataToRead > 0) {
1061-
// Verify that the client is connected.
1062-
if (objResponse.IsClientConnected) {
1063-
// Read the data in buffer
1064-
intLength = objStream.Read(bytBuffer, 0, 10000);
1065-
1066-
// Write the data to the current output stream.
1067-
objResponse.OutputStream.Write(bytBuffer, 0, intLength);
1068-
1069-
// Flush the data to the HTML output.
1070-
objResponse.Flush();
1071-
1072-
bytBuffer = new byte[10001];
1073-
// Clear the buffer
1074-
lngDataToRead = lngDataToRead - intLength;
1075-
} else {
1076-
//prevent infinite loop if user disconnects
1077-
lngDataToRead = -1;
1078-
}
1079-
}
1080-
1081-
} catch (Exception ex) {
1082-
// Trap the error, if any.
1083-
// objResponse.Write("Error : " & ex.Message)
1084-
} finally {
1085-
if ((objStream == null) == false) {
1086-
// Close the file.
1087-
objStream.Close();
1088-
}
1089-
}
1090-
}
975+
objResponse.AppendHeader("Content-Type", MimeMapping.GetMimeMapping(objFile.Extension));
976+
objResponse.AppendHeader("Content-Disposition", "attachment; filename=" + DownloadAs);
977+
objResponse.AppendHeader("Content-Length", objFile.Length.ToString());
978+
objResponse.TransmitFile(objFile.FullName);
979+
}
1091980

1092981
#endregion
1093982

0 commit comments

Comments
 (0)