久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 C# 將可流式內存中文檔 (.docx) 上傳到 FTP?

Upload a streamable in-memory document (.docx) to FTP with C#?(使用 C# 將可流式內存中文檔 (.docx) 上傳到 FTP?)
本文介紹了使用 C# 將可流式內存中文檔 (.docx) 上傳到 FTP?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在嘗試將 MemoryStream 中的 .docx 文件上傳到 FTP

I am trying to upload a .docx file which is in MemoryStream to FTP

但上傳完成后,文件為空.

But when upload is completed, the file is empty.

MemoryStream mms = new MemoryStream();
document2.SaveToStream(mms, Spire.Doc.FileFormat.Docx);

string ftpAddress = "example";
string username = "example";
string password = "example";

using (StreamReader stream = new StreamReader(mms))
{
    // adnu is a random file name.
    WebRequest request =
        WebRequest.Create("ftp://" + ftpAddress + "/public_html/b/" + adnu + ".docx");
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.Credentials = new NetworkCredential(username, password);
    Stream reqStream = request.GetRequestStream();
    reqStream.Close();
}

推薦答案

直接將文檔寫入請求流.使用中間 MemoryStream 沒有意義.StreamReader/StreamWriter 用于處理文本文件,而 .docx 是二進制文件格式,因此也不要使用它們.

Write the document directly to the request stream. There's no point using an intermediate MemoryStream. And StreamReader/StreamWriter are for working with text files, while a .docx is a binary file format, so do not use those either.

WebRequest request =
    WebRequest.Create("ftp://ftp.example.com/remote/path/document.docx");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
using (Stream ftpStream = request.GetRequestStream())
{
    document2.SaveToStream(ftpStream, Spire.Doc.FileFormat.Docx);
}

或使用 WebClient.OpenWrite:

using (var webClient = new WebClient())
{
    const string url = "ftp://ftp.example.com/remote/path/document.docx";
    using (Stream uploadStream = client.OpenWrite(url))
    {
        document2.SaveToStream(uploadStream, Spire.Doc.FileFormat.Docx);
    }
}


你只需要一個中間MemoryStream,如果Spire庫需要一個可搜索的流,FtpWebRequest.GetRequestStream返回的Stream是什么不是.我無法測試.


You will only need an intermediate MemoryStream, if the Spire library requires a seekable stream, what the Stream returned by FtpWebRequest.GetRequestStream is not. I cannot test that.

如果是這樣,請使用:

MemoryStream memoryStream = new MemoryStream();
document2.SaveToStream(memoryStream, Spire.Doc.FileFormat.Docx);

memoryStream.Seek(0, SeekOrigin.Begin);

WebRequest request =
    WebRequest.Create("ftp://ftp.example.com/remote/path/document.docx");
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(username, password);
using (Stream ftpStream = request.GetRequestStream())
{
    memoryStream.CopyTo(ftpStream);
}

或者同樣,您可以像前面的示例一樣使用 WebClient.OpenWrite.

Or again, you can use WebClient.OpenWrite as in the previous example.

另請參閱類似問題壓縮目錄并上傳到 FTP 服務器,而無需在 C# 中本地保存 .zip 文件.

這篇關于使用 C# 將可流式內存中文檔 (.docx) 上傳到 FTP?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 進行身份驗證并跨請求保留自定義聲明)
ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授權不起作用)
ASP Core Azure Active Directory Login use roles(ASP Core Azure Active Directory 登錄使用角色)
How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何獲取守護進程或服務器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發技
.Net Core 2.0 - Get AAD access token to use with Microsoft Graph(.Net Core 2.0 - 獲取 AAD 訪問令牌以與 Microsoft Graph 一起使用)
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
主站蜘蛛池模板: 一区二区在线观看av | 日本久久精品视频 | 日韩高清中文字幕 | 成人免费网站www网站高清 | 国产91久久精品一区二区 | 2020天天操 | 91原创视频在线观看 | 亚洲一区二区三区在线视频 | 亚洲成a人片 | 亚洲午夜视频 | 日韩欧美在线免费 | 在线欧美 | 久久亚洲国产精品日日av夜夜 | 一区二区三区国产好 | 狠狠做六月爱婷婷综合aⅴ 国产精品视频网 | 国产成人免费视频网站高清观看视频 | 成人国产精品久久 | 久久久久久久av | 中文字幕视频在线观看 | 操久久 | 97成人精品| 国产精品www| 99热这里有精品 | 欧美综合色 | 91精品国产一区 | 欧洲一级黄 | 国产在线视频99 | 亚洲精品一区二区三区四区高清 | 亚洲精品在线视频 | 久久国产精品无码网站 | 国产精品网址 | 不卡一区二区在线观看 | 日韩精品成人 | 午夜影视免费片在线观看 | 欧美mv日韩mv国产网站91进入 | 亚洲国产精品区 | 中文字幕高清 | 精品欧美一区二区三区 | 成人网av | 欧日韩在线 | 九九av|