您可以发现此链接对于研究将文件上传到块中的 azure blob 很有用:
http ://wely-lau.net/2012/02/26/uploading-big-files-in-windows-azure-blob-storage-with- putlistblock/
我在这里复制代码以删除外部依赖项:
protected void btnUpload_Click(object sender, EventArgs e)
{
CloudBlobClient blobClient;
var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
container.CreateIfNotExist();
var permission = container.GetPermissions();
permission.PublicAccess = BlobContainerPublicAccessType.Container;
container.SetPermissions(permission);
string name = fu.FileName;
CloudBlockBlob blob = container.GetBlockBlobReference(name);
blob.UploadFromStream(fu.FileContent);
int maxSize = 1 * 1024 * 1024; // 4 MB
if (fu.PostedFile.ContentLength > maxSize)
{
byte[] data = fu.FileBytes;
int id = 0;
int byteslength = data.Length;
int bytesread = 0;
int index = 0;
List<string> blocklist = new List<string>();
int numBytesPerChunk = 250 * 1024; //250KB per block
do
{
byte[] buffer = new byte[numBytesPerChunk];
int limit = index + numBytesPerChunk;
for (int loops = 0; index < limit; index++)
{
buffer[loops] = data[index];
loops++;
}
bytesread = index;
string blockIdBase64 = Convert.ToBase64String(System.BitConverter.GetBytes(id));
using (var ms = new MemoryStream(buffer, true))
blob.PutBlock(blockIdBase64, ms, null);
无效的); blocklist.Add(blockIdBase64); 身份证++; } while (byteslength - bytesread > numBytesPerChunk);
int final = byteslength - bytesread;
byte[] finalbuffer = new byte[final];
for (int loops = 0; index < byteslength; index++)
{
finalbuffer[loops] = data[index];
loops++;
}
string blockId = Convert.ToBase64String(System.BitConverter.GetBytes(id));
using (var ms = new MemoryStream(finalbuffer, true))
blob.PutBlock(blockId, ms, null);
blocklist.Add(blockId);
blob.PutBlockList(blocklist);
}
else
blob.UploadFromStream(fu.FileContent);
}
您还可以在此处找到由 Steve Marx 开发的用于大文件上传(例如)的 silverlight 控件。