iOS从Windows Azure下载BLOB

时间:2014-07-28 18:35:37

标签: php ios azure blob

我正在上传我的文件并将其存储在我的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);

}

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];

}

我的问题是:当我尝试将缓冲区分配到NSString甚至NSData时,它们都是零! 可能是解码Im使用吗?

检索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 个答案:

答案 0 :(得分:0)

解码错误,试试这个......

- (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];


    }