簡體   English   中英

如何通過.net中的多個api傳遞文件

[英]How to pass a file through multiple api in .net

我正在嘗試使用 REST API 從 Sharepoint 下載文件。 因為我的應用程序是用 .Net Core 編寫的,而 CSOM 庫不支持它,所以我在 .Net Framework 中創建了一個“共享點代理”,它是一個托管在 Azure 上的應用程序。 現在我在嘗試下載文件時遇到問題。 我從 Postman 向我在 .Net Core 中的應用程序發送一個請求,它向 sharepoint 代理發送另一個請求,后者(最后)向 Sharepoint REST API 發送一個 GET 請求。 結果,我在 Sharepoint 代理中成為來自 sharepoint REST API 的 Stream,我嘗試將其轉發回我的應用程序。 我不知道應該使用哪種格式來發送文件。 我嘗試了 WebStream、FileStream 和 byte[],但在每種情況下我都得到了一個無法讀取的文件。

.Net Core App 中的下載方法

       public async Task<Stream> DownloadFile(SharePointFileUrl spInfo)
       {
           var restUrl = $"{siteUrl}/downloadFile";

           using (var httpClient = new HttpClient())
           {
               var content = new StringContent(JsonConvert.SerializeObject(spInfo), Encoding.UTF8, "application/json");
               var webResponse = await httpClient.PostAsync(restUrl, content);

               return await webResponse.Content.ReadAsStreamAsync();
           }
       }

Sharepoint 代理中的端點

        public byte[] DownloadFile([FromBody] SharePointFileUrl fileInfo)
        {
            return _spService.DownloadFile(fileInfo.FileUrl);
        }

Sharepoint代理中的下載方法

        public byte[] DownloadFile(string url)
        {
            var restUrl = $"{_siteUrl}/_api/web/GetFileByServerRelativeUrl('/{url}')/$value";
            var request = CreateBaseRequest("GET", restUrl);
            request.Headers.Add("X-RequestDigest", _formDigest);

            WebResponse fileResponse = request.GetResponse();
            var input = fileResponse.GetResponseStream();

            using (MemoryStream ms = new MemoryStream())
            {
                input.CopyTo(ms);
                return ms.ToArray();
            }
        }

預先感謝您的任何幫助。 當然,我已經用谷歌搜索了我的問題,但沒有結果。

在你的 Core 應用程序中試試這個。

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri("<file url string>"));
return File(req.GetResponse().GetResponseStream(), "<Content type>", "<download file name>");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM