簡體   English   中英

嘗試將生成的 pdf 文件上傳到 Azure Blob 存儲時出現 404 錯誤

[英]404 Error when trying to upload a generated pdf file to Azure Blob Storage

這是我得到的錯誤:Microsoft.WindowsAzure.Storage.StorageException:'遠程服務器返回錯誤:(404)未找到。

在以下代碼中:

使用 PDFSharp 生成 PDF 的第一種方法:

[Route("cpd-services/generate-generic-sla/{cpd_services_id}/{userid}")]
    public ActionResult GenerateGenericClientSLA(int cpd_services_id, int userId)
    {
        var genericSLA = m_cpdServicesRepository.GetCPDServicesGenericSubscriptionDetail(cpd_services_id, userId);

        string SLAContent = m_cpdServicesRepository.GetSLATemplateByType(CPDServicesSLAHelpers.GenericClientDraftSLA);

        SLAContent = InsertGenericSLAData(SLAContent, genericSLA);

        var SLATitle = "GenericSLA" + "-" + userId;

        PdfDocument document = PdfGenerator.GeneratePdf(SLAContent, PdfSharp.PageSize.A4);
        PdfGenerateConfig config = new PdfGenerateConfig();
        config.PageSize = PdfSharp.PageSize.A4;
        var file = File(PDF.PDFDocumentToBytes(document), "application/pdf");
        file.FileDownloadName = SLATitle.ToLower() + ".pdf";

        return UploadGenericSLA(file, userId, cpd_services_id, SLATitle);
    }

上傳通用 SLA 方法:

public JsonResult UploadGenericSLA(FileContentResult file, int userId, int CPDServicesId, string sla)
    {
        Storage storage = new Storage(Settings);

        string filename = storage.UploadPDFDocument(file, "documents/cpd-services-service-level-agreement/generic/cpd-" + CPDServicesId + "/" + sla.Trim().ToLower() + ".?");

        int result = m_cpdServicesRepository.AddCPDServicesGenericSLA(file.FileDownloadName.Trim().ToLower(), CPDServicesSLAHelpers.GenericClientDraftSLA, userId, CPDServicesId);

        if (result > 0)
        {
            TempData[CRUDResult.CRUDMessage] = $"{CRUDResult.Success}|SLA has been successfully generated";
            new TelemetryHelper { }.TrackTrace($"SLA Generation - {CPDServicesId}", Microsoft.ApplicationInsights.DataContracts.SeverityLevel.Information);
            return Json(result);
        }
        else
        {
            TempData[CRUDResult.CRUDMessage] = $"{CRUDResult.Failed}|SLA Generation Failed";
            return Json(result);
        }
    }

這反過來會在我的 Storage.cs class 上觸發此方法:

public string UploadPDFDocument(FileContentResult file, string filename)
    {
        return UploadPDFFile($"{Settings.StoragePath}/{Settings.Environment}", file, filename);
    }

protected string UploadPDFFile(string container, FileContentResult file, string filename)
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Settings.AzureStorageConnectionString);
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer blobContainer = blobClient.GetContainerReference(container.ToLower());


        if (filename.EndsWith(".?"))
        {
            int pos = file.FileDownloadName.LastIndexOf(".");
            filename = (filename.Substring(0, filename.Length - 1) + file.FileDownloadName.Substring(pos + 1)).ToLower();
        }

        CloudBlockBlob blob = blobContainer.GetBlockBlobReference(filename.ToLower());

        blob.Properties.ContentType = "application/pdf";

        blob.SetProperties(); //This is where the request to the blob storage fails.

        blob.Metadata.Add("ContentType", "application/pdf");
        blob.Metadata.Add("Size", file.FileContents.Length.ToString());
        blob.Metadata.Add("ContentLength", file.FileContents.Length.ToString());
        blob.Metadata.Add("Filename", filename);

        if (FileExists(container, filename))
        {
            blob.CreateSnapshot();
        }

        blob.UploadFromByteArray(file.FileContents, 0, file.FileContents.Length);

        return filename;
    }

這是 FileExists 方法的代碼:

protected bool FileExists(string container, string filename)
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Settings.AzureStorageConnectionString);
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer blobContainer = blobClient.GetContainerReference(container);

        CloudBlockBlob blob = blobContainer.GetBlockBlobReference(filename);

        return blob.Exists();
    }

我們目前使用 WindowsAzure.Storage - 公司還不想升級...

任何幫助將不勝感激

您可以排除 blob.SetProperties() 方法。

試試看,如果這不起作用,請嘗試為內容類型設置 Blobhttpheaders。 請參考線程以獲取@Gaurav Mantri 建議的可能解決方案。

也嘗試將屬性分成兩步( 參考

BlobProperties blobProperties = blockblob.Properties;

blobProperties.ContentType = "應用程序/pdf";

其他參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM