File.StartUpload 方法
啟動新的區塊上傳工作階段,並上傳的第一個片段。這個方法完成時,不會變更目前的檔案內容。方法是冪 (,因此不會變更結果),只要您uploadId和stream使用相同的值。
上傳工作階段]: 結束您使用CancelUpload(Guid)方法,或當您順利完成上傳工作階段的ContinueUpload(Guid, Int64, Stream)和FinishUpload(Guid, Int64, Stream)方法穿越檔案內容的其餘部分。
例如,您也可以分成三種 10 MB 資料流與一 5 MB 串流並傳 (使用產生的位移的值為 10、 20 和 30 MB) 這種方式傳遞 35 MB 檔案 ︰
StartUpload(GUID, stream1)
ContinueUpload(GUID, 10 MB, stream2)
ContinueUpload(GUID, 20 MB, stream3)
FinishUpload(GUID, 30 MB, stream4)
StartUpload(Guid, Stream)和ContinueUpload(Guid, Int64, Stream)方法會傳回累加的上傳的資料大小位元組,使您可以將這些傳回的值至ContinueUpload(Guid, Int64, Stream)和FinishUpload(Guid, Int64, Stream)後續使用
命名空間: Microsoft.SharePoint.Client
組件: Microsoft.SharePoint.Client.Silverlight (在 Microsoft.SharePoint.Client.Silverlight.dll 中); Microsoft.SharePoint.Client.Phone (在 Microsoft.SharePoint.Client.Phone.dll 中) Microsoft.SharePoint.Client (在 Microsoft.SharePoint.Client.dll 中)
語法
'宣告
Public Function StartUpload ( _
uploadId As Guid, _
stream As Stream _
) As ClientResult(Of Long)
'用途
Dim instance As File
Dim uploadId As Guid
Dim stream As Stream
Dim returnValue As ClientResult(Of Long)
returnValue = instance.StartUpload(uploadId, _
stream)
public ClientResult<long> StartUpload(
Guid uploadId,
Stream stream
)
參數
uploadId
類型:System.Guid上傳工作階段的唯一識別碼。使用此識別項在後續的來電至ContinueUpload(Guid, Int64, Stream)FinishUpload(Guid, Int64, Stream),與CancelUpload(Guid)方法。
stream
類型:System.IO.Stream包含要上傳的第一個片段的資料流。
傳回值
類型:Microsoft.SharePoint.Client.ClientResult<Int64>
上傳資料以位元組為單位的大小。
備註
下列範例示範如何覆寫現有檔案使用StartUpload、 ContinueUpload和FinishUpload方法。
protected void UploadFile(object sender, EventArgs e)
{
var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);
using (var clientContext = spContext.CreateUserClientContextForSPHost())
{
//Get file from Shared Documents library and overwrite it.
var documentsFolder = clientContext.Web.GetFolderByServerRelativeUrl("/Shared Documents");
Microsoft.SharePoint.Client.File uploadFile = documentsFolder.Files.GetByUrl("file name");
clientContext.Load(uploadFile);
clientContext.ExecuteQuery();
//Fetch new version of file from resources. Write the file contents to a stream.
var inputFile = Properties.Resources.<resource file>;
var inputStream = new MemoryStream();
inputStream.Write(inputFile, 0, inputFile.Length);
inputStream.Position = 0;
//Set up size of fragments to upload.
int chunkSize = 1000000;
int index = 0;
Int64 offset = 0;
var myGuid = Guid.NewGuid();
List<byte[]> chunkList = new List<byte[]>();
while ( inputStream.Position < inputStream.Length)
{
byte[] buffer = new byte[chunkSize];
int chunkBytesRead = 0;
while (chunkBytesRead < chunkSize)
{
int bytesRead = inputStream.Read(buffer, chunkBytesRead, chunkSize - chunkBytesRead);
MemoryStream stream = new MemoryStream();
stream.Write(buffer, 0, buffer.Length);
if (bytesRead == 0)
{
break;
}
chunkBytesRead += bytesRead;
if (index == 0)
{
offset = uploadFile.StartUpload(myGuid, stream).Value;
clientContext.ExecuteQuery();
}
else if (inputStream.Position == inputStream.Length)
{
uploadFile.FinishUpload(myGuid, offset, stream);
clientContext.ExecuteQuery();
}
else
{
offset = uploadFile.ContinueUpload(myGuid, offset, stream).Value;
clientContext.ExecuteQuery();
}
}
index++;
}
}
}
這個方法是目前僅適用於Office 365。