IOS 使用ASIHttpRequest 或 MKNetworkKit 上傳圖片到ASP.NET

來源:互聯網
上載者:User

上傳圖片和資料到伺服器 是最基本的需求了,有些學IOS的 可能對伺服器怎麼接收圖片這塊不太瞭解。 所以今天 我把伺服器的代碼 也拷過來了。。。  只會ASP.NET 。。   PHP 和J2EE 應該差不多 都是幾句話的事 IOS端代碼: NSString* path =  [[NSBundlemainBundle]pathForResource:@"iphone1-1-10"ofType:@"png"];#pragma mark 使用ASIHttpRequest 上傳圖片和資料   ASIFormDataRequest* request = [ASIFormDataRequestrequestWithURL:[NSURLURLWithString:@"http://192.168.0.1/IOSUPLOAD/default.aspx"]];    [requestaddFile:pathforKey:@"img"];    [requestaddPostValue:@"asihttp"forKey:@"name"];    [request setCompletionBlock:^{       NSLog(@"%@",request.responseString);    }];    [requestsetFailedBlock:^{           NSLog(@"asi error: %@",request.error.debugDescription);    }];    [request startAsynchronous];#pragma mark 使用MKNetworkKit 上傳圖片和資料    MKNetworkEngine* engine = [[[MKNetworkEnginealloc]init]autorelease];    NSDictionary* postvalues = [NSDictionarydictionaryWithObjectsAndKeys:@"mknetwork",@"name",nil];    MKNetworkOperation* op = [engineoperationWithURLString:@"http://192.168.0.1/IOSUPLOAD/default.aspx"params:postvalueshttpMethod:@"POST"];    [opaddFile:pathforKey:@"img"];    [opaddCompletionHandler:^(MKNetworkOperation *completedOperation) {       NSLog(@"%@",request.responseString);    }errorHandler:^(MKNetworkOperation *completedOperation,NSError *error) {            NSLog(@"mknetwork error : %@",error.debugDescription);    }];    [engineenqueueOperation:op];    //看上去就是跟樣本的差不多         ASP.NET服務端的代碼:[csharp] www.2cto.comprotected void Page_Load(object sender, EventArgs e)  {      if (Request.Files.Count == 0)      {          Response.Write("none file");      }      else      {          HttpPostedFile file = Request.Files["img"];          String filename = Request.Form["name"];          file.SaveAs(MapPath("~/"+filename+".png"));                    Response.Write("ok");      }  }   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.