2014-12-10 98 views
1

我將圖像文件保存到我的應用程序中的文件夾並將其路徑保存在數據庫中。當我在本地運行並且其路徑也正在存儲時,圖片上傳工作正常。當我將它部署到Azure時,它會顯示「處理您的請求時發生錯誤」的錯誤。我的數據庫中有兩張桌子。第一個保存文本的地方在azure上工作正常,但圖片上傳引發了這個錯誤。將圖像保存到不在azure上工作的文件夾

Text saved correctly

enter image description here enter image description here

我使用this代碼,當我在本地運行上傳圖像及其作品的罰款。 需要幫助?

當我做出錯誤關斷模式,它給這個錯誤

+0

一些更詳細的將是一件好事。您是如何部署到Azure的 - 作爲Azure網站,雲服務,虛擬機?你知道如何檢索日誌,所以你可以看到有關錯誤的更多細節? – sh1rts 2014-12-10 23:54:48

+0

我部署爲一個天藍色的網站 – 2014-12-11 07:55:07

+0

不,我只是按照這個鏈接部署到數據庫第一天天藍色http://www.asp.net/mvc/overview/getting-started/database-first-development/publish-to-azure – 2014-12-11 09:16:11

回答

1

代碼使用上傳圖像不會在Azure中運行。試想一下,如果你擴大實例的範圍,這應該如何工作。 您應該使用Azure blob存儲在Azure中上傳文件。 您可以谷歌很好的教程,但要長話短說,你應該做到以下幾點:

1)創建Azure存儲帳戶中manage.windowsazure.com

2)設置存儲連接字符串(你可以從你的存儲賬戶獲得AccountKey)

<configuration> 
    <appSettings> 
     <add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=storagesample;AccountKey=nYV0gln9fT7bvY+rxu2iWAEyzPNITGkhM88J8HUoyofpK7C8fHcZc2kIZp6cKgYRUM74lHI84L50Iau1+9hPjB==" /> 
    </appSettings> 
</configuration> 

在你的代碼

3)添加下面的命名空間

using Microsoft.WindowsAzure.Storage; 
using Microsoft.WindowsAzure.Storage.Auth; 
using Microsoft.WindowsAzure.Storage.Blob; 

4.)初始化存儲連接

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString); 
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); 

5.)創建容器,如果它存在diesn't

// Retrieve a reference to a container. 
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer"); 

// Create the container if it doesn't already exist. 
container.CreateIfNotExists(); 
container.SetPermissions(
    new BlobContainerPermissions { PublicAccess = 
    BlobContainerPublicAccessType.Blob }); 

6.)指定的對象名

//檢索對名爲「myblob」的blob的引用。 CloudBlockBlob blockBlob = container.GetBlockBlobReference(「myblob」);

7.)上傳FATA

MemoryStream的MS =新的MemoryStream();
圖片img = Image.FromStream(model.ImageUpload.InputStream);
img.Save(ms,ImageFormat.Jpeg);

//使用 文件中的內容創建或覆蓋「myblob」blob。 blockBlob.UploadFromStream(ms。ToArray的());

代碼鏈接到您看起來就像這樣:

[HttpPost] [ValidateAntiForgeryToken]公衆的ActionResult 創建(ImageViewModel模型){ VAR validImageTypes =新的String [] { 「圖像/ GIF」, 「圖像/ JPEG」, 「圖像/ PJPEG」, 「圖像/ PNG」 }

if (model.ImageUpload == null || model.ImageUpload.ContentLength == 0) 
{ 
    ModelState.AddModelError("ImageUpload", "This field is required"); 
} 
else if (!imageTypes.Contains(model.ImageUpload.ContentType)) 
{ 
    ModelState.AddModelError("ImageUpload", "Please choose either a GIF, JPG or PNG image. 
} 

if (ModelState.IsValid) 
{ 
    var image = new Image 
    { 
     Title = model.Title, 
     AltText = model.AltText, 
     Caption = model.Caption 
    } 

    if (model.ImageUpload != null && model.ImageUpload.ContentLength > 0) 
    { 

CloudStorageAccount storageAccount = CloudStorageAccount.Parse( ConfigurationManager.ConnectionStrings [「StorageConnectionString」] .ConnectionString); CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); //檢索對容器的引用。 CloudBlobContainer container = blobClient.GetContainerReference(「uploads」);

// Create the container if it doesn't already exist. 
container.CreateIfNotExists(); 
container.SetPermissions(
    new BlobContainerPermissions { PublicAccess = 
    BlobContainerPublicAccessType.Blob }); 

blockBlob = container.GetBlockBlobReference(model.ImageUpload.FileName); MemoryStream ms = new MemoryStream(); MemoryStream ms = new MemoryStream(); MemoryStream ms = new MemoryStream();};
圖片img = Image.FromStream(model.ImageUpload.InputStream);
img.Save(ms,ImageFormat.Jpeg);

//使用 文件中的內容創建或覆蓋「myblob」blob。 blockBlob.UploadFromStream(ms.ToArray()); image.ImageUrl = imageUrl; }

db.Create(image); 
    db.SaveChanges(); 
    return RedirectToAction("Index"); 
} 

return View(model); } 
0

在天青寫入本地物理路徑是不允許的。因此,您需要替換您的保存功能,將這些位寫入Azure Blob Storage或任何其他相關存儲系統等外部存儲系統。

如果你正在尋找一個臨時的本地存儲的實際保存之前做一些快速的處理,那麼你可以使用Azure的本地存儲

相關問題