如何使用带有Web服务的目标c下载文件返回字节数组

时间:2014-05-29 09:12:20

标签: ios iphone objective-c bytearray file-transfer

我有一个iPhone应用程序。在我的登录视图中,我将用户名和密码发送到ASP.NET Web服务,webservice返回JSON。如果用户成功登录,我可以通过Web服务调用另一种方法,您可以在下面看到:

[WebMethod]
public Byte[] GetDocument(string DocumentName)
{
    string strdocPath;
    strdocPath = "path" + DocumentName;

    FileStream objfilestream = new FileStream(strdocPath,FileMode.Open,FileAccess.Read);
    int len = (int)objfilestream.Length;            
    Byte[] documentcontents  = new Byte[len];
    objfilestream.Read(documentcontents,0,len);
    objfilestream.Close();

    return documentcontents;    
} 

现在我的问题是:如何从我的iPhone应用程序中调用此方法并将其作为.sqlite.db文件保存到文档目录

1 个答案:

答案 0 :(得分:0)

@Try this

NSOutputStream *fileOutputStream;
NSString *databasePath = [NSString stringWithFormat:@"%@/Documents/%@",NSHomeDirectory(),@"database.sqlite"];
    fileOutputStream = nil;
    fileOutputStream = [[NSOutputStream alloc] initToFileAtPath:databasePath append:NO];
    [fileOutputStream open];


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

   int bytesWritten  = [fileOutputStream write:[data bytes] maxLength:[data length]];
    NSLog(@" %s bytesWritten == %d  error == %@",__FUNCTION__,bytesWritten,[fileOutputStream streamError]);
}