我目前正在构建一个REST API,该API可以将文件检索为字符串并将其放入列表中。当前代码的最终结果是能够将所有Strings(但所有字符串)都发布在一行中,我想应该是可以预期的。我想知道是否有更有效的方法来构建流。在其他示例中,我注意到人们会冲洗,处置或关闭流,但不知道这里是否有必要。另外,有没有一种方法可以列出字符串,而不是将所有字符串都放在一行中?
Dim ms As MemoryStream
Dim response As HttpResponseMessage = Request.CreateResponse(HttpStatusCode.BadRequest)
ms = New MemoryStream()
Dim buffer As [Byte]() = fileColl.SelectMany(Function(item) Text.Encoding.UTF8.GetBytes(item)).ToArray()
Dim contentLength = buffer.Length
Dim status = HttpStatusCode.OK
response = Request.CreateResponse(status)
response.Content = New StreamContent(New MemoryStream(buffer))
response.Content.Headers.ContentType = New MediaTypeHeaderValue("text/plain")
response.Content.Headers.ContentLength = contentLength
Dim cd As ContentDispositionHeaderValue = Nothing
response.Content.Headers.ContentDisposition = cd
Return response
更新 了解如何为每个字符串分配自己的行
Dim buffer As [Byte]() = fileColl.SelectMany(Function(item) Text.Encoding.UTF8.GetBytes(item + Environment.NewLine)).ToArray()