【问题标题】:How to download file using objective c with webservice returns byte array如何使用带有 web 服务的目标 c 下载文件返回字节数组
【发布时间】:2014-05-29 17:48:19
【问题描述】:

我有一个 iPhone 应用程序。在我的登录视图中,我将用户名和密码发送到 ASP.NET 网络服务,网络服务返回 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 文件保存到文档目录中

【问题讨论】:

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


    【解决方案1】:

    @试试这个

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

    【讨论】:

    • 但是我如何调用我的 Web 服务,例如服务路径是 http.//test.com/myservice.asmx 并且它获取字符串参数。我如何从 ios 调用它?
    • 字符串参数表示是文件下载的url 例如:xyz.14.111.19/application/database.zip
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多