phpazurefile-uploadwindows-runtimewindows-phone-8.1

Why after I uploaded an image to Azure I cannot access it?


I'm developing a Universal App for Windows Phone 8.1 but I'm using a PHP Page to get recognize some patterns from an Image that I uploaded to my service.

I have discovered that after I uploaded X image to Azure, I cannot use it. I'm using WebMatrix to develop my PHP Page and when I refresh it, it doesn't show me the images that I uploaded however when I try to publish something and I select the option: "Delete files on the remote server that are not on my computer." I can see my images. This is an example of my PHP code:

$uploaddir = getcwd();
$uploadfile = $uploaddir . "/" . basename($_FILES['userfile']['name']);

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    chmod($uploadfile, 0755);
    $Test = new Display($_FILES['userfile']['name']);
    echo '{"result": "' . $Test->getNumber($_REQUEST['color'], false) . '"}';
    //unlink($uploadfile);
} else {
   echo '{"result": "-1"}';
}

I'd like to know what could be my bug because I don't understand why I can access from the URL, too to the bit I cannot use it, maybe it's how I assigned the permissions but with or without the chmod, it doesn't change at all. I have even tried other hostings and the problem is the same when I enter the File Manager, there are only my PHP files and it doesn't allow me to manage the image.

This is my Windows Phone code to upload the Image if it's necessary:

byte[] ConvertBitmapToByteArray()
{
    WriteableBitmap bmp = bitmap;

    using (Stream stream = bmp.PixelBuffer.AsStream())
    {
        MemoryStream memoryStream = new MemoryStream();
        stream.CopyTo(memoryStream);
        return memoryStream.ToArray();
    }
}

public async Task<string> Upload()
{
    try
    {
        using (var client = new HttpClient())
        {
            using (var content =
                new MultipartFormDataContent())
            {
                byte[] data = ConvertBitmapToByteArray();

                using (var stream = new InMemoryRandomAccessStream())
                {
                    // encoder *outputs* to stream
                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, stream);

// encoder's input is the bitmap's pixel data
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, 
    (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, 96, 96, data);

await encoder.FlushAsync();

content.Add(new StreamContent(stream.AsStream()), "userfile", fileNewImage);

                using (
                   var message =
                       await client.PostAsync("http://xplace.com/uploadtest.php", content))
                {
                    var input = await message.Content.ReadAsStringAsync();

                    return input;
                }
            }
          }
        }
    }
    catch (Exception ex)
    {
        return null;
    }
}

Thanks for your worthy knowledge and experience.


Solution

  • Create a blob storage account, and add a public container. In your action to save the file, store the file in you blob storage container. Then you can access the image as you would with any other image.

    Here is a tutorial on Azure: http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/

    Also, you cannot create folders in a container, but you could use a naming convention on the blobrefname to create the idea of a container. Also, you can attach a domain to the cloud service if you want the URL to have a certain look.

    READ YOUR QUESTION AGAIN - And it looks like it's more on the client side.

    Here is what I usually do to attach a file to a MultipartFormDataContent:

    MultipartFormDataContent content = new MultipartFormDataContent();
    
    FileInfo info = new FileInfo(currFileLoc);    
    string contentMediaType = null;
    
    //This is a Dictionary<string, string> that takes in the file 
    //extension, and returns the media type (e.g. "image/jpeg")
    GlobalVariables.ApprovedMediaTypes.TryGetValue(
                                     info.Extension.ToLower()
                                     , out contentMediaType);
    
    //If the dictionary doesn't return a result 
    //then it's not a supported file type
    if (contentMediaType == null)
        throw new Exception(
                String.Format("The file \"{0}\" is an unsupported file type."
                              , info.Name));
    
    ByteArrayContent currFile = new ByteArrayContent(File.ReadAllByte(currFileLoc));
    
    currFile.Headers.ContentType = new MediaTypeWithQualityHeaderValue(contentMediaType);
    
    content.Add(currFile, currFileLoc, currFileLoc);
    

    The I make my call. Maybe you found another option with blob storage. Finally, if you load large files, you may want to look into uploading in chunks.