Sawant
Sawant

Reputation: 395

How to upload an image to .ashx url in ios

I have searched a lot on google as well as on stackoverflow but did not get any satisfactory solution which works for me.

I have to upload an image on some particular url which is ending with extension .ashx. I have seen how to upload on php server but here i am not getting any clue.

Please help me by providing some sample code.

Upvotes: 0

Views: 1183

Answers (2)

βhargavḯ
βhargavḯ

Reputation: 9836

As per my understanding aspx is the page and the .ashx is the code file which response back the output, in string format and .ashx file is a web handler. A web handler file works just like an aspx file....

So we consider.ashx same as .aspx then this code should work for you(which is running for me for .aspx page). This is making request to .net server.

iOS

    UIImage *img = [UIImage imageNamed:@"test.png"];
    NSData *imageData = UIImageJPEGRepresentation ( img , 90 );

    NSString *urlString =@"www.xyz.com/ImageUpload.aspx?filename=test";

    NSLog(@"IMAGE_UPLOAD_URL -------------> %@",urlString);

    NSMutableURLRequest *request = [[[ NSMutableURLRequest alloc ] init ] autorelease ];
    [request setURL :[ NSURL URLWithString :urlString]];
    [request setHTTPMethod : @"POST" ];
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [ NSString stringWithFormat : @"multipart/form-data; boundary=%@" ,boundary];
    [request addValue :contentType forHTTPHeaderField : @"Content-Type" ];
    /*  body of the post */
    NSMutableData *body = [ NSMutableData data ];
    [body appendData :[ NSData dataWithData :imageData]];
    [request setHTTPBody :body];
    NSData *returnData = [ NSURLConnection sendSynchronousRequest :request returningResponse : nil error : nil ];

    NSString *returnString = [[ NSString alloc ] initWithData :returnData encoding : NSUTF8StringEncoding ];
    InfoLog(@"_______  IMAGE_UPLOAD response -------------> .%@.",returnString);

.NET

Retrieving image like this for .aspx page

    if (Request.QueryString["filename"] != null)
    {
        string filename = Request.QueryString["filename"].ToString();
        string saveFilePath = ConfigurationManager.AppSettings["CPSBImageFolder"].ToString();
        //string saveFilePath = Server.MapPath("~/images");
        saveFilePath = saveFilePath + filename;
        Stream objStream = Request.InputStream;
        StreamReader objStreamReader = new StreamReader(objStream);
        Image image = Image.FromStream(objStreamReader.BaseStream, true);
        ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();
        EncoderParameters param = new EncoderParameters(1);
        param.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
        image.Save(saveFilePath, info[1], param);
        Response.Write("true");
    }

Not sure but hope this give you a clue.

Upvotes: 1

alloc_iNit
alloc_iNit

Reputation: 5183

Verify that the variable(NSData) you are using to upload image is not null & your dot net server is receiving request.

Upvotes: 0

Related Questions