Skip to content

Commit

Permalink
Merge pull request #31 from juvander/FixFor30
Browse files Browse the repository at this point in the history
Improves and simplifies code for download logic
  • Loading branch information
valadas authored Jan 16, 2020
2 parents 03f2e69 + d9672b6 commit 9d424ef
Showing 1 changed file with 13 additions and 124 deletions.
137 changes: 13 additions & 124 deletions Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -904,13 +904,8 @@ public void DownloadFile(string ItemID)
public void DownloadFile(string ItemID, string target)
{
RepositoryController repository = new RepositoryController();
RepositoryInfo objRepository = null;
int i = 0;
int iExtension = 0;
int iStart = 0;
int iEnd = 0;
RepositoryInfo objRepository = null;
string strDownloadURL = "";
string strURLTarget = "";

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

Expand Down Expand Up @@ -968,126 +963,20 @@ public void DownloadFile(string ItemID, string target)
}


private void StreamFile(string FilePath, string DownloadAs)
{
DownloadAs = DownloadAs.Replace(" ", "_");

System.IO.FileInfo objFile = new System.IO.FileInfo(FilePath);
System.Web.HttpResponse objResponse = System.Web.HttpContext.Current.Response;
objResponse.ClearContent();

objResponse.ClearHeaders();
objResponse.AppendHeader("Content-Disposition", "attachment; filename=" + DownloadAs);
objResponse.AppendHeader("Content-Length", objFile.Length.ToString());

string strContentType = null;
switch (objFile.Extension) {
case ".txt":
strContentType = "text/plain";
break;
case ".htm":
case ".html":
strContentType = "text/html";
break;
case ".rtf":
strContentType = "text/richtext";
break;
case ".jpg":
case ".jpeg":
strContentType = "image/jpeg";
break;
case ".gif":
strContentType = "image/gif";
break;
case ".bmp":
strContentType = "image/bmp";
break;
case ".mpg":
case ".mpeg":
strContentType = "video/mpeg";
break;
case ".avi":
strContentType = "video/avi";
break;
case ".pdf":
strContentType = "application/pdf";
break;
case ".doc":
case ".dot":
strContentType = "application/msword";
break;
case ".csv":
case ".xls":
case ".xlt":
strContentType = "application/vnd.msexcel";
break;
default:
strContentType = "application/octet-stream";
break;
}
objResponse.ContentType = strContentType;
WriteFile(objFile.FullName);
private void StreamFile(string FilePath, string DownloadAs)
{
DownloadAs = DownloadAs.Replace(" ", "_");

objResponse.Flush();
objResponse.Close();

}

public static void WriteFile(string strFileName)
{
System.Web.HttpResponse objResponse = System.Web.HttpContext.Current.Response;
System.IO.Stream objStream = null;

// Buffer to read 10K bytes in chunk:
byte[] bytBuffer = new byte[10001];

// Length of the file:
int intLength = 0;

// Total bytes to read:
long lngDataToRead = 0;

try {
// Open the file.
objStream = new System.IO.FileStream(strFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
System.IO.FileInfo objFile = new System.IO.FileInfo(FilePath);
System.Web.HttpResponse objResponse = System.Web.HttpContext.Current.Response;
objResponse.ClearContent();
objResponse.ClearHeaders();

// Total bytes to read:
lngDataToRead = objStream.Length;

objResponse.ContentType = "application/octet-stream";

// Read the bytes.
while (lngDataToRead > 0) {
// Verify that the client is connected.
if (objResponse.IsClientConnected) {
// Read the data in buffer
intLength = objStream.Read(bytBuffer, 0, 10000);

// Write the data to the current output stream.
objResponse.OutputStream.Write(bytBuffer, 0, intLength);

// Flush the data to the HTML output.
objResponse.Flush();

bytBuffer = new byte[10001];
// Clear the buffer
lngDataToRead = lngDataToRead - intLength;
} else {
//prevent infinite loop if user disconnects
lngDataToRead = -1;
}
}

} catch (Exception ex) {
// Trap the error, if any.
// objResponse.Write("Error : " & ex.Message)
} finally {
if ((objStream == null) == false) {
// Close the file.
objStream.Close();
}
}
}
objResponse.AppendHeader("Content-Type", MimeMapping.GetMimeMapping(objFile.Extension));
objResponse.AppendHeader("Content-Disposition", "attachment; filename=" + DownloadAs);
objResponse.AppendHeader("Content-Length", objFile.Length.ToString());
objResponse.TransmitFile(objFile.FullName);
}

#endregion

Expand Down

0 comments on commit 9d424ef

Please sign in to comment.