为什么我将图像上传到 Azure 后无法访问它?
Why after I uploaded an image to Azure I cannot access it?
我正在为 Windows Phone 8.1 开发通用应用程序,但我正在使用 PHP 页面从我上传到我的服务的图像中识别一些模式.
我发现将X镜像上传到Azure后,无法使用。我正在使用 WebMatrix 开发我的 PHP 页面,当我刷新它时,它没有显示我上传的图像,但是当我尝试发布一些东西时,我 select 选项:“删除远程服务器上不在我的计算机上的文件。” 我可以看到我的图像。这是我的 PHP 代码示例:
$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"}';
}
我想知道我的错误是什么,因为我不明白为什么我可以从 URL 访问,甚至我不能使用它,也许这就是我分配权限的方式但是有或没有 chmod,它根本不会改变。我什至尝试过其他主机,当我进入文件管理器时问题是一样的,只有我的 PHP 个文件,它不允许我管理图像。
这是我的 Windows Phone 代码,用于在需要时上传图片:
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;
}
}
感谢您宝贵的知识和经验。
创建一个 blob 存储帐户,并添加一个 public 容器。在保存文件的操作中,将文件存储在 blob 存储容器中。然后您可以像访问任何其他图像一样访问该图像。
这是一个关于 Azure 的教程:http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/
此外,您不能在容器中创建文件夹,但您可以使用 blobrefname 的命名约定来创建容器的概念。此外,如果您希望 URL 具有特定外观,您可以将域附加到云服务。
再读一遍你的问题 - 看起来更多是在客户端。
以下是我通常将文件附加到 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);
我打电话。也许您找到了另一个 blob 存储选项。最后,如果您加载大文件,您可能需要考虑分块上传。
我正在为 Windows Phone 8.1 开发通用应用程序,但我正在使用 PHP 页面从我上传到我的服务的图像中识别一些模式.
我发现将X镜像上传到Azure后,无法使用。我正在使用 WebMatrix 开发我的 PHP 页面,当我刷新它时,它没有显示我上传的图像,但是当我尝试发布一些东西时,我 select 选项:“删除远程服务器上不在我的计算机上的文件。” 我可以看到我的图像。这是我的 PHP 代码示例:
$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"}';
}
我想知道我的错误是什么,因为我不明白为什么我可以从 URL 访问,甚至我不能使用它,也许这就是我分配权限的方式但是有或没有 chmod,它根本不会改变。我什至尝试过其他主机,当我进入文件管理器时问题是一样的,只有我的 PHP 个文件,它不允许我管理图像。
这是我的 Windows Phone 代码,用于在需要时上传图片:
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;
}
}
感谢您宝贵的知识和经验。
创建一个 blob 存储帐户,并添加一个 public 容器。在保存文件的操作中,将文件存储在 blob 存储容器中。然后您可以像访问任何其他图像一样访问该图像。
这是一个关于 Azure 的教程:http://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/
此外,您不能在容器中创建文件夹,但您可以使用 blobrefname 的命名约定来创建容器的概念。此外,如果您希望 URL 具有特定外观,您可以将域附加到云服务。
再读一遍你的问题 - 看起来更多是在客户端。
以下是我通常将文件附加到 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);
我打电话。也许您找到了另一个 blob 存储选项。最后,如果您加载大文件,您可能需要考虑分块上传。