如何从iphone应用程序上传godaddy服务器上的音频文件? [英] How to upload an audio file on godaddy server from iphone app?

查看:125
本文介绍了如何从iphone应用程序上传godaddy服务器上的音频文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了一个iPhone应用程序来上传一个音频文件。

我也在asp.net中使webervices支持这个应用程序的其他功能,并发布在godaddy服务器。

我想从我的iphone应用程序上传/保存该服务器上的音频文件。



我搜索了很多代码,但是它们并不相关,并且是有用的。

那么我怎么能这样做呢?

如何获得服务器路径,我们可以上传文件?

请提供一些示例代码为iPhone做到这一点。



pre> NSString * fileName = @myAudio;
NSData * audioFile = [NSData dataWithContentsOfFile:@myAudio.ext]; //一些音频
NSString * urlString = @http://www.something.com/someservice;
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL * url = [NSURL URLWithString:urlString];
NSString * strPostLength = [NSString stringWithFormat:@%d,[audioFile length]];
NSMutableURLRequest * uRequest = [[NSMutableURLRequest alloc] init];
[uRequest setURL:url];
[uRequest setHTTPMethod:@POST];
if(audioFile)
{
NSMutableData * body = [NSMutableData data];

NSString * boundary = [NSString stringWithString:@--------------------------- 14737809831466499882746641449];
NSString * contentType = [NSString stringWithFormat:@multipart / form-data; boundary =%@,boundary];
[uRequest addValue:contentType forHTTPHeaderField:@Content-Type];
$ b $ *
现在可以创建邮件正文
* /

[body appendData:[[NSString stringWithFormat:@\r \ n - %@ \\\\ n,边界] dataUsingEncoding:NSUTF8StringEncoding]];
[uRequest setValue:strPostLength forHTTPHeaderField:@Content-Length];
[body appendData:[[NSString stringWithFormat:@Content-Disposition:form-data; name = \userfile \; filename = \%@。ext \\r\ n,fileName] dataUsingEncoding:NSUTF8StringEncoding]];

// userfile - >在服务器代码中给出的变量名称...


$ b $

  [body appendData:[[NSString stringWithString:@Content-Type:application / octet-stream \r\\\
\r\\\
] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:audioFile]];
[body appendData:[[NSString stringWithFormat:@\r\\\
- %@ - \r\\\
,boundary] dataUsingEncoding:NSUTF8StringEncoding]];

//将帖子正文设置为reqeust
[uRequest setHTTPBody:body];
}

NSURLConnection * uConn = [[NSURLConnection alloc] initWithRequest:uRequest delegate:self];
if(uConn)
{
NSLog(@无法连接到URL);
}
uRequest = nil;

我希望这可以帮助您解决您的问题...

I have made an iphone app to upload an audio file.

I have also made webervices in asp.net to support other functionality for this app and published it on godaddy server.

I want to upload/save an audio file on that server from my iphone app.

I have searched many codes but they are not relevant and usefull.

So how can i do this ?

How to get serverpath where we can upload the file?

Please provide some sample code for iphone to do this.

解决方案

This is how I POST a file (can be any file , image, video, audio anything) to a web-service

NSString *fileName = @"myAudio";
    NSData *audioFile = [NSData dataWithContentsOfFile:@"myAudio.ext"]; //some audio
    NSString *urlString = @"http://www.something.com/someservice";
    urlString = [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlString];
    NSString *strPostLength = [NSString stringWithFormat:@"%d", [audioFile length]];  
    NSMutableURLRequest *uRequest = [[NSMutableURLRequest alloc] init];  
    [uRequest setURL:url];  
    [uRequest setHTTPMethod:@"POST"];  
    if (audioFile)
    {
        NSMutableData *body = [NSMutableData data];

        NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        [uRequest addValue:contentType forHTTPHeaderField: @"Content-Type"];

        /*
         now lets create the body of the post
         */

        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [uRequest setValue:strPostLength forHTTPHeaderField:@"Content-Length"];  
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.ext\"\r\n",fileName] dataUsingEncoding:NSUTF8StringEncoding]];

//userfile -> variable name given in the server code...

        [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[NSData dataWithData:audioFile]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

        // setting the body of the post to the reqeust
        [uRequest setHTTPBody:body];
    }

    NSURLConnection *uConn=[[NSURLConnection alloc] initWithRequest:uRequest delegate:self];  
    if (uConn)   
    {  
        NSLog(@"Failed to connect to url"); 
    } 
    uRequest = nil;

I hope this will help you in solving your issue...

这篇关于如何从iphone应用程序上传godaddy服务器上的音频文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆