I'm uploading my file and storing it on my blob service like this
我正在上传我的文件并将其存储在我的blob服务上
- (IBAction)upLoadButton:(UIButton *)sender{
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"imgToServer.jpg"], 90);
NSString *urlString = @"http://myWebSite/createBlobDirect.php";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"userfile\"; filename=\"imageFromIOS\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(@"RETURN > %@",returnString);
}
The BLOB is created with Success, although I can't read the data when I download the data from the BLOB, this is my code:
BLOB是使用Success创建的,虽然我从BLOB下载数据时无法读取数据,这是我的代码:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *convertedBuffer = [[NSString alloc]initWithData:self.buffer encoding:NSUTF8StringEncoding];
NSLog(@"%@",convertedBuffer);
//THESE OBJECTS FAILS TO BE ALLOC!
imageFromServer = [[NSData alloc]initWithBase64EncodedData:self.buffer options:NSUTF8StringEncoding];
UIImage *image = [[UIImage alloc]initWithData:imageFromServer];
[imageView setImage:image];
}
MY PROBLEM IS: When I try to alloc the buffer into NSString or even NSData, they are nil! Could it be the decoding Im using?
我的问题是:当我尝试将缓冲区分配到NSString甚至NSData时,它们都是零!可能是我使用的解码吗?
PHP File that retrieves the BLOB
用于检索BLOB的PHP文件
<?php
//require_once 'vendor\autoload.php';
require_once __DIR__ . '/vendor/autoload.php';
use WindowsAzure\Common\ServicesBuilder;
use WindowsAzure\Blob\Models\CreateContainerOptions;
use WindowsAzure\Blob\Models\PublicAccessType;
use WindowsAzure\Common\ServiceException;
// Create blob REST proxy.
$connectionString = "[WORKING CONNECTION STRING]";
$blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString);
$blob_name = "myblob";
try {
// Get blob.
$blob = $blobRestProxy->getBlob("CONTAINER", $blob_name);
fpassthru($blob->getContentStream());
}
catch(ServiceException $e){
// Handle exception based on error codes and messages.
// Error codes and messages are here:
// http://msdn.microsoft.com/en-us/library/windowsazure/dd179439.aspx
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
?>
1 个解决方案
#1
0
The decoding is wrong, try this...
解码是错误的,试试这个......
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSData *tempData = [self.buffer base64EncodedDataWithOptions:NSUTF8StringEncoding];
// NSString *convertedBuffer = [[NSString alloc] initWithData:self.buffer encoding:NSUTF8StringEncoding];
NSString *convertedBuffer = [[NSString alloc] initWithData:tempData encoding:NSUTF8StringEncoding];
NSLog(@"%@",convertedBuffer);
imageFromServer = [[NSData alloc]initWithBase64EncodedData:tempData options:NSUTF8StringEncoding];
UIImage *image = [[UIImage alloc]initWithData:imageFromServer];
[imageView setImage:image];
}
#1
0
The decoding is wrong, try this...
解码是错误的,试试这个......
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSData *tempData = [self.buffer base64EncodedDataWithOptions:NSUTF8StringEncoding];
// NSString *convertedBuffer = [[NSString alloc] initWithData:self.buffer encoding:NSUTF8StringEncoding];
NSString *convertedBuffer = [[NSString alloc] initWithData:tempData encoding:NSUTF8StringEncoding];
NSLog(@"%@",convertedBuffer);
imageFromServer = [[NSData alloc]initWithBase64EncodedData:tempData options:NSUTF8StringEncoding];
UIImage *image = [[UIImage alloc]initWithData:imageFromServer];
[imageView setImage:image];
}