kpanchal Ответов: 2

Презентация Openxml: как загрузить файл(web httpresponse) без сохранения в физическом пути.


 i replace text in presentationDocumentNew 
 Save file on Physical path - Its worked
  presentationDocumentNew.SaveAs(tmpFilePath);
  presentationDocumentNew.Close();

Question: OpenXml Presentation(PPT-pptx) i don't want to save in physical part. I just want to download file directly from browser using HttpResponse
 I try many way to But its not worked. File downloed but its correpted.
    Try: 1) presentationDocumentNew.PresentationPart.GetStream().CopyTo(Response.OutputStream);
	     2) take Stream or Memory stream or FileStream, set in response
		    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
          Response.AddHeader("Content-Disposition", "attachment; filename=" + "cccc.pptx");
          ......etc
 Not worked all


Что я уже пробовал:

<pre>PresentationDocument presentationDocumentNew = null;


using (PresentationDocument presentationDocument = PresentationDocument.Open(file, false))
                   {
                       presentationDocumentNew = (PresentationDocument)presentationDocument.Clone();
                       presentationDocument.Close();
                   }


if (presentationDocumentNew != null)
                    {

// i replace text in presentationDocumentNew 
// Save file on Physical path - Its worked
//  presentationDocumentNew.SaveAs(tmpFilePath);
//  presentationDocumentNew.Close();

//Question: i don't want to save in physical part. I just want to download file directly from browser using HttpResponse
// I try many way to But its not worked. File downloed but its correpted.
//    Try: 1) presentationDocumentNew.PresentationPart.GetStream().CopyTo(Response.OutputStream);
//	     2) take Stream or Memory stream or FileStream, set in response
//		    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
//          Response.AddHeader("Content-Disposition", "attachment; filename=" + "cccc.pptx");
//          ......etc
// Not worked all

}

2 Ответов

Рейтинг:
0

RickZeeland

Может ты попробуешь служба WebClient, смотрите примеры здесь: https://www.dotnetperls.com/webclient[^]

Или попробовать WebRequest, смотрите пример здесь: Как запросить данные с помощью класса WebRequest | Microsoft Docs[^]


Рейтинг:
0

Richard Deeming

Это решение на StackOverflow[^] предназначен для документов Word, но здесь будет применяться тот же подход:

var memoryStream = new MemoryStream();

using (FileStream fileStream = File.OpenRead(file))
{
    fileStream.CopyTo(memoryStream);
}

memoryStream.Position = 0;

using (PresentationDocument presentationDocument = PresentationDocument.Open(memoryStream, true))
{
    ... Change the presentation here ...
    
    presentationDocument.Close();
}

memoryStream.Position = 0;

Response.ContentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
Response.AddHeader("Content-Disposition", "attachment; filename=" + "cccc.pptx");
memoryStream.CopyTo(Response.OutputStream);