Click here to Skip to main content
15,889,735 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to resolve network related or instance specific error Pin
Member 1200209515-Oct-15 22:37
Member 1200209515-Oct-15 22:37 
GeneralRe: How to resolve network related or instance specific error Pin
NeillJam15-Oct-15 23:05
NeillJam15-Oct-15 23:05 
AnswerRe: How to resolve network related or instance specific error Pin
NeillJam15-Oct-15 23:24
NeillJam15-Oct-15 23:24 
QuestionHow do I download big file of applications Pin
Member 1201610615-Oct-15 14:58
Member 1201610615-Oct-15 14:58 
SuggestionRe: How do I download big file of applications Pin
Richard MacCutchan15-Oct-15 23:03
mveRichard MacCutchan15-Oct-15 23:03 
GeneralRe: How do I download big file of applications Pin
Member 1201610615-Oct-15 23:31
Member 1201610615-Oct-15 23:31 
GeneralRe: How do I download big file of applications Pin
molesworth16-Oct-15 2:43
molesworth16-Oct-15 2:43 
AnswerRe: How do I download big file of applications Pin
Richard Deeming16-Oct-15 2:44
mveRichard Deeming16-Oct-15 2:44 
You have several options:

TransmitFile is the preferred method:
C#
string physicalPath = ListBox1.SelectedValue;
if (!string.IsNullOrWhiteSpace(physicalPath))
{
    FileInfo fileToSend = new FileInfo(fileToSend);
    if (fileToSend.Exists)
    {
        System.Web.HttpContext context = System.Web.HttpContext.Current;
        context.Response.Clear();
        context.Response.ClearHeaders();
        context.Response.ClearContent();
        context.Response.ContentType = "application/octet-stream";
        context.Response.AddHeader("content-length", fileToSend.Length.ToString());
        context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlPathEncode(fileToSend.Name));
        context.Response.TransmitFile(fileToSend.FullName);
        context.Response.Flush();
        context.ApplicationInstance.CompleteRequest();
    }
}

WriteFile will work in the same way, but can have problems with large files[^].

Reading the file in small blocks requires more code, and won't perform as well as TransmitFile:
C#
// (Same code as before, up to and including the "Content-Disposition" header)

int bytesRead;
byte[] buffer = new byte[10000];
using (Stream fileStream = fileToSend.OpenRead())
{
    while (context.Response.IsClientConnected && (bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
    {
        context.Response.OutputStream.Write(buffer, 0, bytesRead);
        context.Response.Flush();
    }
}

context.ApplicationInstance.CompleteRequest();




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


QuestionI try to dynamically generate a name for my objects Pin
Member 1019526215-Oct-15 10:41
Member 1019526215-Oct-15 10:41 
AnswerRe: I try to dynamically generate a name for my objects Pin
BillWoodruff15-Oct-15 18:56
professionalBillWoodruff15-Oct-15 18:56 
GeneralRe: I try to dynamically generate a name for my objects Pin
Member 1019526219-Oct-15 7:11
Member 1019526219-Oct-15 7:11 
QuestionAccessing dynamically generated textboxes Pin
KakitaIppatsu15-Oct-15 5:43
KakitaIppatsu15-Oct-15 5:43 
AnswerRe: Accessing dynamically generated textboxes Pin
OriginalGriff15-Oct-15 6:11
mveOriginalGriff15-Oct-15 6:11 
GeneralRe: Accessing dynamically generated textboxes Pin
KakitaIppatsu15-Oct-15 6:39
KakitaIppatsu15-Oct-15 6:39 
GeneralRe: Accessing dynamically generated textboxes Pin
OriginalGriff15-Oct-15 7:06
mveOriginalGriff15-Oct-15 7:06 
AnswerRe: Accessing dynamically generated textboxes Pin
BillWoodruff15-Oct-15 6:52
professionalBillWoodruff15-Oct-15 6:52 
GeneralRe: Accessing dynamically generated textboxes Pin
KakitaIppatsu15-Oct-15 7:47
KakitaIppatsu15-Oct-15 7:47 
QuestionThe “Microsoft.CodeAnalysis.BuildTasks.Csc” task could not be loaded from the assembly Pin
Member 1204569215-Oct-15 3:31
Member 1204569215-Oct-15 3:31 
QuestionTime to draw 2 usercontrols on a form is depend on each other. Pin
LeHuuTien14-Oct-15 16:10
LeHuuTien14-Oct-15 16:10 
AnswerRe: Time to draw 2 usercontrols on a form is depend on each other. Pin
BillWoodruff14-Oct-15 20:37
professionalBillWoodruff14-Oct-15 20:37 
GeneralRe: Time to draw 2 usercontrols on a form is depend on each other. Pin
LeHuuTien14-Oct-15 22:27
LeHuuTien14-Oct-15 22:27 
GeneralRe: Time to draw 2 usercontrols on a form is depend on each other. Pin
BillWoodruff14-Oct-15 22:38
professionalBillWoodruff14-Oct-15 22:38 
GeneralRe: Time to draw 2 usercontrols on a form is depend on each other. Pin
LeHuuTien14-Oct-15 23:10
LeHuuTien14-Oct-15 23:10 
GeneralRe: Time to draw 2 usercontrols on a form is depend on each other. Pin
Pete O'Hanlon14-Oct-15 23:13
mvePete O'Hanlon14-Oct-15 23:13 
GeneralRe: Time to draw 2 usercontrols on a form is depend on each other. Pin
LeHuuTien14-Oct-15 23:22
LeHuuTien14-Oct-15 23:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.