locked
How to implement Azure Storage Uploading procedure? RRS feed

  • Question

  • User-157580668 posted

    I have googled and implement this code to 'upload.aspx' file

        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("lawimage");
    
        // Retrieve reference to a blob named "myblob".
        CloudBlockBlob blockBlob = container.GetBlockBlobReference("myblob");
    
        // Create or overwrite the "myblob" blob with contents from a local file.
        using (var fileStream = System.IO.File.OpenRead(name))
        {
            blockBlob.UploadFromStream(fileStream);
        }

    But the server said this error statement :

    Error posting the file to /FileMan/upload.aspx?CKEditor=Document&CKEditorFuncNum=2&langCode=ko<br>Response status: 500

    There are few azure storage uploading referece related to MVC but I can't find none about Webpages

    Does anybody ever succeed?

    For your information,

    my 'web.config' is

        <appSettings>
            <add key="StorageConnectionString" value="DefaultEndpointsProtocol=https;AccountName=[MyAccountName];AccountKey=[MyKey]" />
        </appSettings>

    and this is my 'upload.aspx' full source code

    <%@ Page Language="C#" Strict="true"%>
    <script runat="server">
    protected void  Page_Load(object sender, EventArgs e)
    {
    	Response.Write(processUpload());
    }
    
    // Upload script for CKEditor.
    // Use at your own risk, no warranty provided. Be careful about who is able to access this file
    // The upload folder shouldn't be able to upload any kind of script, just in case.
    // If you're not sure, hire a professional that takes care of adjusting the server configuration as well as this script for you.
    // (I am not such professional)
    
    private String processUpload()
    {
        // Step 1: change the true for whatever condition you use in your environment to verify that the user
        // is logged in and is allowed to use the script
        // if (true)
        //    return sendError("You're not allowed to upload files");
    
        // Step 2: Put here the full absolute path of the folder where you want to save the files:
        // You must set the proper permissions on that folder
        String basePath = Server.MapPath("~/App_Data/Images/");
    
        // Step 3: Put here the Url that should be used for the upload folder (it the URL to access the folder that you have set in $basePath
        // you can use a relative url "/images/", or a path including the host "http://example.com/images/"
        // ALWAYS put the final slash (/)
        String baseUrl = Server.MapPath("~/App_Data/Images/");
    
        // Done. Now test it!
    
        // No need to modify anything below this line
        //----------------------------------------------------
    
        // ------------------------
        // Input parameters: optional means that you can ignore it, and required means that you
        // must use it to provide the data back to CKEditor.
        // ------------------------
    
        // Optional: instance name (might be used to adjust the server folders for example)
        String CKEditor = HttpContext.Current.Request["CKEditor"] ;
    
        // Required: Function number as indicated by CKEditor.
        String funcNum = HttpContext.Current.Request["CKEditorFuncNum"] ;
    
        // Optional: To provide localized messages
        String langCode = HttpContext.Current.Request["langCode"] ;
    
        // ------------------------
        // Data processing
        // ------------------------
    
       	int total;
        try
        {
    		total = HttpContext.Current.Request.Files.Count;
    	}
    	catch (Exception e)
    	{
    		return  sendError("Error uploading the file");
    	}
        if (total==0)
            return sendError("No file has been sent");
    
        if (!System.IO.Directory.Exists(basePath))
            return sendError("basePath folder doesn't exists");
    
    	//Grab the file name from its fully qualified path at client
    	HttpPostedFile theFile = HttpContext.Current.Request.Files[0];
    
    	String strFileName = theFile.FileName;
        if (strFileName=="")
            return sendError("File name is empty");
    
        String sFileName = System.IO.Path.GetFileName(strFileName);
    
        String name = System.IO.Path.Combine(basePath, sFileName);
    	theFile.SaveAs(name);
    
        String url = baseUrl + sFileName.Replace("'", "\'");
    
    
    
        // ------------------------
        // Write output
        // ------------------------
    
        return "<scr" + "ipt type='text/javascript'> window.parent.CKEDITOR.tools.callFunction(" + funcNum + ", '" + url + "', '')</scr" + "ipt>";
    }
    
    private String sendError(String msg)
    {
        String funcNum = HttpContext.Current.Request["CKEditorFuncNum"] ;
    	return "<scr" + "ipt type='text/javascript'> window.parent.CKEDITOR.tools.callFunction(" + funcNum + ", '', '" + msg + "')</scr" + "ipt>";
    }
    </script>

    Well. I regret to hosting my website to Azure, 

    If hosting to Azure is more complicated than other Hosting service provicder, then why use Azure?

    Wednesday, March 2, 2016 10:58 AM

Answers

  • User753101303 posted

    Hi,

    Hide immediately your account detail !!!

    500 is a server side exception and for now it is unclear where it happens (on the web server or on the Azure side ?). If your code hides exception details, get first the full exception to better understand what happens.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 2, 2016 2:33 PM

All replies

  • User753101303 posted

    Hi,

    Hide immediately your account detail !!!

    500 is a server side exception and for now it is unclear where it happens (on the web server or on the Azure side ?). If your code hides exception details, get first the full exception to better understand what happens.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 2, 2016 2:33 PM
  • User-157580668 posted

    Thank you!

    I hide my account name and key.

    Thursday, March 3, 2016 12:52 AM
  • User-157580668 posted

    I have solved myself!   By using Visual Studio, I found a bugging point. olleh!!

    Even though I don't like Visual Studio, Visual Studio is very powerful tool Tongue Out  

    Maby It's heavyness is worth for that.

    This code will work!

    <%@ Import namespace="System.Configuration" %>
    <%@ Import namespace="Microsoft.WindowsAzure" %>
    <%@ Import namespace="Microsoft.WindowsAzure.Storage" %>
    <%@ Import namespace="Microsoft.WindowsAzure.Storage.Auth" %>
    <%@ Import namespace="Microsoft.WindowsAzure.Storage.Blob" %>
    
    
    ......

            HttpPostedFile theFile = HttpContext.Current.Request.Files[0];
    // Azure Upload // Retrieve storage account from connection string. StorageCredentials sc = new StorageCredentials("[MyStorageName]", "[MyKey]"); CloudStorageAccount storageAccount = new CloudStorageAccount(sc, false); // Create the blob client. CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient(); // Retrieve reference to a previously created container. CloudBlobContainer container = blobClient.GetContainerReference("lawimage"); // Retrieve reference to a blob named "myblob". CloudBlockBlob blockBlob = container.GetBlockBlobReference(sFileName); // Create or overwrite the "myblob" blob with contents from a local file. using (var fileStream = theFile.InputStream) { blockBlob.UploadFromStream(fileStream); }

    .....

    Friday, March 4, 2016 5:02 PM