public HttpWebResponse PushFileToWistia(byte[] contentFileByteArray, string fileName)
{
StringBuilder postDataBuilder = new StringBuilder();
postDataBuilder.Append("I am appending all the wistia config and setting here");
byte[] postData = null;
using (MemoryStream postDataStream = new MemoryStream())
{
byte[] postDataBuffer = Encoding.UTF8.GetBytes(postDataBuilder.ToString());
postDataStream.Write(postDataBuffer, 0, postDataBuffer.Length);
postDataStream.Write(contentFileByteArray, 0, contentFileByteArray.Length);
postDataBuffer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--");
postDataStream.Write(postDataBuffer, 0, postDataBuffer.Length);
postData = postDataStream.ToArray();
}
ServicePointManager.Expect100Continue = false;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(AppConfig.WistiaCustomCourseBucket);
request.Method = "POST";
request.Expect = String.Empty;
request.Headers.Clear();
request.ContentType = "multipart/form-data; boundary=" + boundary;
request.ContentLength = postData.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(postData, 0, postData.Length); //for file > 100mb this call throws and error --the requet was aborted. the request was canceled.
requestStream.Flush();
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
return response;
}
上述代碼適用於視頻文件mp4小於50mb。但是,當我嘗試上傳一個100MB的文件時,它會拋出異常(請求被中止)。我需要支持的文件大小高達1.5GB所以現在我不確定這種方法對於如此大的文件大小上傳是否正確。任何建議在正確的方向將是有益的...謝謝(我想上傳文件到Wistia服務器) 在這一行發生異常 - requestStream.Write(postData,0,postData.Length);寫請求流時請求被中止錯誤
我試圖改變的web.config的設置,但沒有奏效: 的httpRuntime targetFramework = 「4.5」 的maxRequestLength = 「2048576」 executionTimeout = 「12000」 requestLengthDiskThreshold = 「1024」
----- -async呼叫-------
MemoryStream wistiaFileStream = null;
using (MemoryStream postDataStream = new MemoryStream())
{
postDataStream.Write(contentFileByteArray, 0, contentFileByteArray.Length);
wistiaFileStream = postDataStream;
postDataStream.Flush();
postDataStream.Close();
}
Stream requestStream = await request.GetRequestStreamAsync();
await requestStream.WriteAsync(wistiaMetadata, 0, wistiaMetadata.Length);
using (wistiaFileStream)
{
byte[] wistiaFileBuffer = new byte[500*1024];
int wistiaFileBytesRead = 0;
while (
(wistiaFileBytesRead =
await wistiaFileStream.ReadAsync(wistiaFileBuffer, 0, wistiaFileBuffer.Length)) != 0)
{
await requestStream.WriteAsync(wistiaFileBuffer, 0, wistiaFileBytesRead);
await requestStream.FlushAsync();
}
await requestStream.WriteAsync(requestBoundary, 0, requestBoundary.Length);
}
我沒有從磁盤讀取文件我從HTML文件上傳中獲取文件。我不得不修改上面的解決方案,以適應我的代碼。它正確運行異步調用witohut任何錯誤。它將成功消息返回給客戶端Ux。但我沒有看到WISTIA服務器上的任何文件..它運行如此之快的1.5 GB文件,我不認爲它實際上發送文件內容WISTIA ..有沒有一種方法,我們可以阻止所有其他的執行,直到我們完成異步調用並獲得成功狀態...我在問題 – Scorpio
@Scorpio中添加了編輯的代碼您的實現沒有拋出異常嗎?如果我正確閱讀,當您開始閱讀時,wistiaFileStream會關閉。如果你真的必須將文件保存爲字節數組(這是危險的,因爲在10個並行請求的情況下,最終可能會分配15 GB),我建議直接從該數組寫入請求。我會嘗試編輯我的答案以表明這一點。 – tpeczek
@Scorpio爲不同的方法和一些一般建議添加了一個示例 – tpeczek