web-dev-qa-db-ja.com

MemoryStreamを応答オブジェクトに書き込む

次のコードを使用して、MemoryStreamオブジェクトにあるpptxをストリーミングしていますが、それを開くとPowerPointで修復メッセージが表示されます。MemoryStreamをResponseオブジェクトに書き込む正しい方法は何ですか?

HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.AppendHeader("Content-Type", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}.pptx;", getLegalFileName(CurrentPresentation.Presentation_NM)));                
response.BinaryWrite(masterPresentation.ToArray());
response.End();
36
Ali

私は同じ問題を抱えていましたが、唯一の解決策は次のとおりでした:

Response.Clear();
Response.ContentType = "Application/msword";
Response.AddHeader("Content-Disposition", "attachment; filename=myfile.docx");
Response.BinaryWrite(myMemoryStream.ToArray());
// myMemoryStream.WriteTo(Response.OutputStream); //works too
Response.Flush();
Response.Close();
Response.End();
65
playful

たとえば、Stream、FileStream、またはMemoryStreamを取得できると仮定すると、これを実行できます。

Stream file = [Some Code that Gets you a stream];
var filename = [The name of the file you want to user to download/see];

if (file != null && file.CanRead)
{
    context.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
    context.Response.ContentType = "application/octet-stream";
    context.Response.ClearContent();
    file.CopyTo(context.Response.OutputStream);
}

それは私の作業コードの一部からのコピーアンドペーストなので、コンテンツタイプはあなたが探しているものではないかもしれませんが、応答へのストリームの書き込みは最後の行のトリックです。

11
Ian Robertson

MemoryStreamでPowerPointプレゼンテーションを作成する代わりに、Response.OutputStreamに直接書き込みます。この方法では、コンポーネントが出力をネットワークソケットストリームに直接ストリーミングするため、サーバーのメモリを無駄にする必要はありません。したがって、このプレゼンテーションを生成している関数にMemoryStreamを渡す代わりに、単にResponse.OutputStreamを渡します。

7
Darin Dimitrov

これで試してください

Response.Clear();
Response.AppendHeader("Content-Type", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
Response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}.pptx;", getLegalFileName(CurrentPresentation.Presentation_NM)));
Response.Flush();                
Response.BinaryWrite(masterPresentation.ToArray());
Response.End();
4
Nudier Mena

最初にメモリストリームに書き込む必要があり、次にメモリストリームメソッド "WriteTo"を使用して、以下のコードに示すようにページのレスポンスに書き込むことができます。

   MemoryStream filecontent = null;
   filecontent =//CommonUtility.ExportToPdf(inputXMLtoXSLT);(This will be your MemeoryStream Content)
   Response.ContentType = "image/pdf";
   string headerValue = string.Format("attachment; filename={0}", formName.ToUpper() + ".pdf");
   Response.AppendHeader("Content-Disposition", headerValue);

   filecontent.WriteTo(Response.OutputStream);

   Response.End();

FormNameは指定されたfileNameです。このコードは、PopUpを呼び出すことにより、生成されたPDFファイルをダウンロード可能にします。

3
Deepak Kothari

End、close、flush、System.Web.HttpContext.Current.ApplicationInstance.CompleteRequest()のすべてのバリエーションを試しましたが、どれも機能しませんでした。

次に、ヘッダーにコンテンツの長さを追加しました:Response.AddHeader( "Content-Length"、asset.File_Size.ToString());

この例では、アセットはFile_SizeというInt32を持つクラスです

これは私のために働いたが、他には何もしなかった。

1
Norwood

同じ問題がありました。これを試してください:MemoryStreamにコピー->ファイルを削除->ダウンロード。

string absolutePath = "~/your path";
try {
    //copy to MemoryStream
    MemoryStream ms = new MemoryStream();
    using (FileStream fs = File.OpenRead(Server.MapPath(absolutePath))) 
    { 
        fs.CopyTo(ms); 
    }

    //Delete file
    if(File.Exists(Server.MapPath(absolutePath)))
       File.Delete(Server.MapPath(absolutePath))

    //Download file
    Response.Clear()
    Response.ContentType = "image/jpg";
    Response.AddHeader("Content-Disposition", "attachment;filename=\"" + absolutePath + "\"");
    Response.BinaryWrite(ms.ToArray())
}
catch {}

Response.End();
0
Alexander SS

私にとっての問題は、ダウンロード前にストリームがOriginに設定されていないことでした。

Response.Clear();
Response.ContentType = "Application/msword";
Response.AddHeader("Content-Disposition", "attachment; filename=myfile.docx");

//ADDED THIS LINE
myMemoryStream.Seek(0,SeekOrigin.Begin);

myMemoryStream.WriteTo(Response.OutputStream); 
Response.Flush();
Response.Close();
0
Ryan Bennett