本文介绍了使用ASMX文件从iPhone上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林在这里奋斗了很多,
我试着去从iPhone上传图片到web服务使用ASMX(VB.net)(IIS)服务器的文件夹

Im struggling here for a lot,Im trying to upload an image from iphone to (iis)server folder using webservice asmx(VB.net)

香港专业教育学院搜查了很多,最后用下面的code

Ive searched a lot and finally used the following code

- (IBAction)btnPostImages_Clicked:(id)sender  {

NSMutableURLRequest *request;

request = [[[NSMutableURLRequest alloc] init] autorelease];

[request setHTTPMethod:@"POST"];

[request setURL:[NSURL URLWithString:@"http://192.168.0.2/digita/digitacampus.asmx/SaveImage"]];

NSString *boundary = [NSString stringWithString:@"---------------------------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:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"%@\"\r\n",@"recFile",@"image.jpg"]dataUsingEncoding:NSUTF8StringEncoding]];


[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[NSData dataWithData:dt]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];

NSURLConnection *conn=[[NSURLConnection alloc] initWithRequest:request delegate:self];

}

dt为JPEG格式重新presentation形象NSData的

dt is NSData of image in jpeg representation

和WebMethod的如下:

and the webmethod as follows

<WebMethod()> _
Public Function SaveImage(ByVal recFile As String) As String
    Dim file As HttpPostedFile = HttpContext.Current.Request.Files("recFile")
    Dim targetFilePath As String = HttpContext.Current.Server.MapPath("") + file.FileName
    file.SaveAs(targetFilePath)
    Return file.FileName.ToString()
End Function

什么都没有发生。

Nothing happened

就是上面的WebMethod正确

Is the above webmethod correct

我也只是尝试如下

    - (IBAction)btnPostImages_Clicked:(id)sender  {

NSURL *url = [NSURL URLWithString:@"http://192.168.0.2/digita/digitacampus.asmx/SaveImage"];

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

[req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

[req addValue:@"image/jpeg" forHTTPHeaderField:@"Content-Type"];

[req setHTTPMethod:@"POST"];

[req setHTTPBody:dt];

NSURL *conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
}

我不知道该怎么办别的

I dont know what to do else

什么,我没有错误

我怎样才能识别图像已经上传成功与否

How can I identify the image was successfully uploaded or not

推荐答案

是网址是否正确?我想应该是这样的:?digitacampus.asmx OP = SaveImage也许digitalcampus.asmx

Is the URL correct? I think it should be something like: digitacampus.asmx?op=SaveImage and maybe digitalcampus.asmx

您必须将字符串转换为FILESTREAM这样的,例如:

You have to convert the String to an filestream like this for example:

Dim fs As FileStream
fs.Write(System.Convert.FromBase64String(recFile), 0, pdfLength)

这篇关于使用ASMX文件从iPhone上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 23:03