gpt4 book ai didi

asp.net-mvc-3 - 如何在Windows Azure平台上传图片: best approach

转载 作者:行者123 更新时间:2023-12-03 00:37:56 27 4
gpt4 key购买 nike

我有一个包含图像上传的注册表单,但当我在 Windows Azure 服务器中上传我的包应用程序时,它不起作用。

服务器中的图片地址如下所示:

F:\sitesroot\0\Uploads\Users\9259826_2121813246965_1294840438_2490950_6619588_n.jpg

如果我有这样的图像网址,及其相对路径:

http://dealma.cloudapp.net/Uploads/Users/9259826_2121813246965_1294840438_2490950_6619588_n.jpg

我已经解决了这个问题。

我当前用于上传的代码是这样的:

if (userImg != null && userImg.ContentLength > 0)
{
try
{
var fileName = Url.Encode(userImg.FileName);
//no overwrite files
var pathToCheck = Server.MapPath("~/Uploads/Users/" + fileName);
var savePath = Server.MapPath("~/Uploads/Users/");
var tempfileName = fileName;
int counter = 2;

while (System.IO.File.Exists(pathToCheck))
{
tempfileName = counter.ToString() + fileName;
pathToCheck = savePath + tempfileName;
counter++;
}

fileName = tempfileName;

var finalImg = Path.Combine(savePath, fileName);
userImg.SaveAs(finalImg);

//Img name
userSet.Picture = finalImg;
userSet.Thumbnail = finalImg;
}
catch (Exception ex)
{
Response.Write("Não foi possível fazer upload do arquivo: " + ex.Message);
}
}

有人知道如何解决这个问题吗?

最佳答案

正如 corvus 所说,您正在写入“本地存储”,该存储是 volatile 的,并且不会在虚拟机的多个实例之间共享。

Blob 存储可让您存储任意文件、图像等。每个项目都存储在其自己的 Blob 中。您还有“容器”的概念 - 将其视为顶级目录文件夹。没有嵌套容器,但您可以使用名称中的路径字符来模拟它们(暂时跳过此步骤,因为您需要快速解决方案)。

如果您下载 Windows Azure Platform Training Kit看一下实验“云服务简介”,它显示了一个留言板应用程序,其中照片被上传到 blob 存储。您将了解如何设置存储帐户,以及编写代码以将文件推送到 blob 而不是本地文件系统。以下是示例的片段:

初始化 blob 客户端,并设置容器来存储文件:

var storageAccount = 
CloudStorageAccount.FromConfigurationSetting("DataConnectionString");

// create blob container for images
blobStorage = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobStorage.GetContainerReference("uploads");
container.CreateIfNotExist();

现在,在上传处理程序中,您将写入 blob 而不是本地文件系统:

 string uniqueBlobName = string.Format("uploads/image_{0}{1}",
Guid.NewGuid(), Path.GetExtension(UserImg.FileName));
CloudBlockBlob blob = blobStorage.GetBlockBlobReference(uniqueBlobName);
blob.Properties.ContentType = UserImg.PostedFile.ContentType;
// note: there are several blob upload methods -
// choose the best one that fits your app
blob.UploadFromStream(UserImg.FileContent);

下载平台培训套件后,您将看到完整的工作示例。

关于asp.net-mvc-3 - 如何在Windows Azure平台上传图片: best approach,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8425824/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com