Почему мы используем Response.ClearHeaders ()? - PullRequest
6 голосов
/ 06 июля 2011

Я скопировал фрагмент кода для отправки файлов в браузер.Я не знаю, почему мы используем строки, написанные ниже, потому что удаление их не имеет никакого значения в моей среде разработки.

Спасибо

Ответы [ 3 ]

10 голосов
/ 06 июля 2011

Response.Clear ();

Если вы уже что-то записали в буфер, вам нужно очистить, чтобы посторонний контент не включался.

Response.ClearHeaders ();

Если тип содержимого был указан ранее, например, вы, вероятно, этого не хотите.Может быть уже задано любое количество заголовков HTTP - еще один хороший пример - контроль кэша.

Response.Buffer = false;

Нет смысла буферизовать вывод, если вы готовы выгрузить файл ... просто отправьте его и нетратить память.

2 голосов
/ 06 июля 2011

Response.ClearHeaders гарантирует, что заголовки не отправляются клиенту. Вам это нужно, потому что перед этой функцией или событием страница могла послать несколько заголовков, например, тип контента или контроль кэша. Вам нужно Response.Clear, потому что страница могла отобразить некоторый html в буфере.

0 голосов
/ 04 сентября 2018
***

> HttpResponse.Buffer Property

***
Gets or sets a value indicating whether to buffer output and send it after the complete response is finished processing.
true if the output to client is buffered; otherwise, false.

***

> HttpResponse.Clear Method

***

Clears all content output from the buffer stream

The following example sets the ContentType property for the response to image/jpeg, calls the Clear method to remove other content that might be attached to the response, and then sets the BufferOutput property to true so that the complete page will be processed before any content is sent to the requesting client.

// Set the page's content type to JPEG files
// and clears all content output from the buffer stream.
Response.ContentType = "image/jpeg";
Response.Clear();

// Buffer response so that page is sent
// after processing is complete.
Response.BufferOutput = true;


***

> HttpResponse.ClearHeaders Method

***

Clears all headers from the buffer stream.

The following example calls the ClearHeaders method to ensure that no headers are sent with the current response. This technique can be especially important if the ASP.NET response is generating an image, such as a JPEG file. In this example the ContentType property is set to image/jpeg.


// Clear headers to ensure none
// are sent to the requesting browser
// and set the content type.
Response.ClearHeaders();
Response.ContentType = "image/jpeg";

-------------------------------------------------------------------------
summary :
asp.net keeps the header in a collection (Response.Headers) and the content in an output stream (Response.Output).

  Response.ClearHeaders - clears the current header collection
  Response.Clear - resets the output stream
  Response.ClearContent call Clear and is just a better name

all three will fail if Response.Buffer == false, and any output was been written. 
...