Using asmx file for uploading image from iphone

2019-04-02 13:55发布

问题:

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

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 is NSData of image in jpeg representation

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

Is the above webmethod correct

I have also tried simply as below

    - (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

whats the mistake that I did

How can I identify the image was successfully uploaded or not

回答1:

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

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)