Sono in procinto di scrivere un servizio WCF che consentirà a un sito Web ASP.Net di recuperare i file (in base a this article). Il mio problema è che quando restituisco il flusso, è vuoto.Restituzione di un flusso da File.OpenRead()
Per semplicità, ho isolato il codice in un semplice WinForms app per cercare di trovare qual è il problema con la restituzione di un torrente, e questo è il codice:
private Stream TestStream()
{
Stream fs = File.OpenRead(@"c:\testdocument.docx");
return fs;
}
// This method converts the filestream into a byte array so that when it is
// used in my ASP.Net project the file can be sent using response.Write
private void Test()
{
System.IO.MemoryStream data = new System.IO.MemoryStream();
System.IO.Stream str = TestStream();
str.CopyTo(data);
byte[] buf = new byte[data.Length];
data.Read(buf, 0, buf.Length);
}
Il risultato di questo codice è che buf
è lungo 12.587 byte (la lunghezza corretta del file) ma contiene solo 0.
Il documento di Word si apre senza problemi se lo provo, mi manca qualcosa di ovvio?
Sei in esecuzione come amministratore? Prova a estrarre il documento da "Documenti" o da un'altra cartella diversa da root. – keyboardP
@keyboard - un buon consiglio ma produrrebbe un'eccezione, non '0's e lunghezza corretta. –
@HenkHolterman - Ah si, è vero. – keyboardP