2017-12-06 26 views
-1

私のasp.netアプリケーションでルーティングされたURL(外部サイト)によってアクセスされるpdfをダウンロードしたいと考えています。 これを行う方法はありますか?C#の外部サイトのURLからPDFファイルを読むには

現状:ルーティングされたURLは、企業の内部サイトである (joomlaのサイト)http://example/sites/index.php/2011-10-30-12-29-04/finish/11/1234 このリンク私はルーティングされたURLを使って自分のアプリケーション(PdfReader)でこのPDFファイルを取得する必要がありpdfファイル にユーザーをリダイレクトです。


更新#1:

あなたはちょうど私が私のpdfReaderにコンテンツストリームを渡すために必要な、(私は元の質問でこれを追加しました)提案として、私は私のコードにいくつかの変更を行いました。

:新PdfReaderとして 薄暗いpdfReader(= contentStream ISP)が次のように 問題が解決されました、私はcontentstreamに合格しなければならなかった。しかし、それはまだそのダウンロードが..


更新#2失敗した私を示してい

多くのおかげで...

Public Async Function GetPDFFromCompanyWebsite() As Task(Of HttpResponseMessage) 

    Using client As HttpClient = New HttpClient() 
     Dim msg As HttpResponseMessage = Await client.GetAsync("http://example/sites/index.php/2011-10-30-12-29-04/finish/4/4088") 
     If msg.IsSuccessStatusCode Then 


      Dim contentStream = Await msg.Content.ReadAsStreamAsync() 

      Dim pdfReader As New PdfReader(isp:=contentStream) 
      Dim MST As MemoryStream = New MemoryStream() 
      Dim pdfStamper As New PdfStamper(pdfReader, MST) 
      For pageIndex As Integer = 1 To pdfReader.NumberOfPages 
       Dim pageRectangle As Rectangle = pdfReader.GetPageSizeWithRotation(pageIndex) 
       Dim pdfData As PdfContentByte = pdfStamper.GetOverContent(pageIndex) 

       pdfData.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 40) 
       Dim graphicsState As New PdfGState() 


       graphicsState.FillOpacity = 0.1F 
       pdfData.SetGState(graphicsState) 

       pdfData.SetColorFill(BaseColor.BLUE) 

       Dim bf As BaseFont = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED) 

       pdfData.SetFontAndSize(bf, pageRectangle.Width/25) 
       pdfData.BeginText() 
       pdfData.SetFlatness(1000) 

       Dim windowsuser As String = User.Identity.Name.Substring(4) 
       windowsuser = windowsuser + "  " + windowsuser + "  " + windowsuser + "  " + windowsuser + "  " + windowsuser + "  " + windowsuser + "  " + windowsuser 
       ' pdfData.ShowTextAligned(Element.ALIGN_BOTTOM, User.Identity.Name, 100, 100, 45) 
       Response.Write("width:height: " + pageRectangle.Width.ToString + "/" + pageRectangle.Height.ToString) 
       pdfData.ShowTextAligned(Element.ALIGN_CENTER, windowsuser, pageRectangle.Width/2, pageRectangle.Height/2, 45) 
       pdfData.ShowTextAligned(Element.ALIGN_CENTER, windowsuser, pageRectangle.Width/4, pageRectangle.Height - (pageRectangle.Height/4), 45) 
       pdfData.ShowTextAligned(Element.ALIGN_CENTER, windowsuser, pageRectangle.Width - (pageRectangle.Width/4), pageRectangle.Height/4, 45) 
       pdfData.ShowTextAligned(Element.ALIGN_CENTER, windowsuser, (3 * pageRectangle.Width)/8, pageRectangle.Height - ((3 * pageRectangle.Height)/8), 45) 
       pdfData.ShowTextAligned(Element.ALIGN_CENTER, windowsuser, pageRectangle.Width - ((3 * pageRectangle.Width)/8), (3 * pageRectangle.Height)/8, 45) 




       pdfData.EndText() 

      Next 
      pdfStamper.Close() 
      Dim bytesInStream As Byte() = MST.ToArray() 

      MST.Close() 

      Response.Clear() 
      Response.ClearContent() 
      Response.ClearHeaders() 
      Response.ContentType = "application/pdf" 
      Response.AddHeader("content-disposition", "attachment;filename=File.pdf") 
      Response.BufferOutput = True 
      Response.Cache.SetCacheability(HttpCacheability.NoCache) 
      Response.BinaryWrite(bytesInStream) 
      Response.End() 
      Response.Close() 

      '  End Using 
     End If 

     Return msg 
    End Using 
End Function 
+0

HttpClientクラスを使用してGETリクエストを作成し、結果をByteArrayに格納してください。 –

+0

あなたはその方法を説明できますか –

答えて

0

は、C#でのHttpClientクラスを使ってあなたの会社のウェブサイトにGETリクエストを作ってみましょう。あなたはラインに沿って何かをすることができます。

using System.Net.Http; 
using System.IO; 

public async Task<HttpResponseMessage> GetPDFFromCompanyWebsite() 
{ 
string currentDirectory = System.Web.Hosting.HostingEnvironment.MapPath("~"); 
string filePath = Path.Combine(currentDirectory, "App_Data", "someDocument.pdf"); 

using(HttpClient client = new HttpClient()) 
{ 
    HttpResponseMessage msg = await client.GetAsync($"http://example/sites/index.php/2011-10-30-12-29-04/finish/11/1234"); 

    if(msg.IsSuccessStatusCode) 
    { 
    using(var file = File.Create(filePath)) 
    { 
     // create a new file to write to 
     var contentStream = await msg.Content.ReadAsStreamAsync(); // get the actual content stream 
     await contentStream.CopyToAsync(file); // copy that stream to the file stream 
     await file.FlushAsync(); // flush back to disk before disposing 
    } 
    } 
    return msg; 
} } 
+0

ありがとうKunal、私はあなたの提案(私は元の質問に追加)として私のコードにいくつかの変更を行った、ちょうど私は自分のpdfReaderにコンテンツストリームを渡す必要があったメモリストリーム。しかし、まだダウンロードが失敗したことがわかります。 –

+0

[Postman](https://app.getpostman.com/app/download/win64?_ga=2.87234652.557795504.1512549768-1694700739.1504503127)からGETリクエストを送信し、回答があるかどうかを確認して、会社のリンクを確認してみてくださいか否か? –

+0

あなたのメソッドを(ファイルとして書くために)試してみたので、リンクがうまくいきました。 –

関連する問題