Hay*_*ore 1 .net c# azure azure-storage-blobs
我试图使用azures blob存储实现以下功能.
奇怪的是,我只能使用一个工作,因为它会导致另一个工作,这取决于我的代码的顺序.
HttpPostedFile image = Request.Files["froalaImage"];
if (image != null)
{
string fileName = RandomString() + System.IO.Path.GetExtension(image.FileName);
string companyID = Request.Form["companyID"].ToLower();
// Retrieve storage account from connection string.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(companyID);
// Create the container if it doesn't already exist.
container.CreateIfNotExists();
// Retrieve reference to a blob named "filename".
CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);
// Create or overwrite the blob with contents from a local file.
using (image.InputStream)
{
blockBlob.UploadFromStream(image.InputStream);
byte[] fileData = null;
using (var binaryReader = new BinaryReader(image.InputStream))
{
fileData = binaryReader.ReadBytes(image.ContentLength);
}
string base64ImageRepresentation = Convert.ToBase64String(fileData);
// Clear and send the response back to the browser.
string json = "";
Hashtable resp = new Hashtable();
resp.Add("link", "data:image/" + System.IO.Path.GetExtension(image.FileName).Replace(@".", "") + ";base64," + base64ImageRepresentation);
resp.Add("imgID", "BLOB/" + fileName);
json = JsonConvert.SerializeObject(resp);
Response.Clear();
Response.ContentType = "application/json; charset=utf-8";
Response.Write(json);
Response.End();
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码会将文件上传到azure的blob存储,但base64字符串将为空.
但如果我把线放在线blockBlob.UploadFromStream(image.InputStream);下面string base64ImageRepresentation = Convert.ToBase64String(fileData);
我将获得base64字符串没有问题,但文件没有正确上传到azure的blob存储.
也许你需要在第一次使用后重置你的流位置?
image.InputStream.Seek(0, SeekOrigin.Begin);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
596 次 |
| 最近记录: |