久久久久久久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模板網(wǎng)!

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

相關文檔推薦

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屋-程序員軟件開發(fā)技
.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 超時)
主站蜘蛛池模板: 色黄视频在线 | 亚洲视频一区 | 成人免费黄色片 | 神马久久久久久久久久 | 日韩高清国产一区在线 | 欧美做暖暖视频 | 国产成人啪免费观看软件 | 欧美综合一区二区三区 | 天天操天天干天天曰 | 福利视频网站 | a级毛片基地 | 高清国产一区二区 | 欧美激情一区二区三区 | 成人精品一区二区三区中文字幕 | 男人午夜视频 | 欧美久久视频 | 91精品国产91久久久 | 亚洲视频免费在线观看 | 一级毛片视频在线观看 | 国产亚洲精品美女久久久久久久久久 | 久久婷婷av | 亚洲福利视频一区二区 | 欧美一区二区在线播放 | 久久国产高清视频 | 亚洲国产精品一区二区三区 | 亚洲欧美激情四射 | 欧美精品一区二区三区视频 | 欧美电影在线观看网站 | 国产精品1区2区3区 欧美 中文字幕 | 欧美一级黄| 国产亚洲精品综合一区 | 欧美激情第一区 | 久久久成人精品 | 欧美一区二区在线 | 精品久久久一区 | 亚洲综合精品 | 一区二区三区中文字幕 | 国产精品99 | 天堂一区在线观看 | 国内精品视频在线 | 国产午夜精品一区二区三区 |