Link to home
Start Free TrialLog in
Avatar of BrianFord
BrianFordFlag for United States of America

asked on

Upload image to asp.net server using Swift 2 in iOS

I am trying to upload an image from iOS using Swift to an IIS web server (ASP.NET)

The code below works fine when posting to IIS ' Server A' but when I try to post to a different server 'Server B', even though the code on both server is 'exactly' the same.

'Server A' successfully saves the image to my folder but 'Server B' says no file was received.

Swift Code:
=============
  @IBAction func uploadPhoto(sender: AnyObject) {
        
        print ("Start upload photo")
        
        // ================================
        // Upload the photo to the server
        // ================================

        let imageData = UIImageJPEGRepresentation(imageView.image!, 0.5)
        var filename:String = ""
        filename = txtBarcode.text! + ".png"
        let url = NSURL(string: "http://www.SERVER-A.com/UploadHandler.ashx")
        let request = NSMutableURLRequest(URL: url!)
        request.HTTPMethod = "POST"
        request.HTTPBody = imageData
        
        let response: AutoreleasingUnsafeMutablePointer<NSURLResponse?>=nil
        let error: AutoreleasingUnsafeMutablePointer<NSErrorPointer?> = nil

        try! NSURLConnection.sendSynchronousRequest(request, returningResponse: response)
        
        print(response)
        }
    }

Open in new window



The UploadHander.ashx code:
============================
Imports System.IO
Imports System.Drawing

Public Class Handler : Implements IHttpHandler

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        Dim result As String = String.Empty

        If context.Request.Files.Count > 0 Then
            result = "File Processed"
            Dim files As HttpFileCollection = context.Request.Files

            For i As Integer = 0 To files.Count - 1
                Dim file As HttpPostedFile = files(i)
                Dim fname As String
                Dim fpath As String
                Dim saveAsPath As String

                fname = file.FileName

                'Save Large Image
                fpath = context.Server.MapPath("~/Images/stock/large/")
                saveAsPath = Path.Combine(fpath, fname)
                file.SaveAs(saveAsPath)

                'Save thumbnail
                Dim thumbimage As Bitmap
                Dim originalimage As Bitmap
                Dim width As Integer = 200
                Dim height As Integer = 200
                Dim newwidth As Integer
                Dim newheight As Integer

                originalimage = Image.FromFile(saveAsPath) 'Get the file that was just saved
                If originalimage.Width > originalimage.Height Then
                    newheight = originalimage.Height / originalimage.Width * height
                    newwidth = width
                Else
                    newheight = height
                    newwidth = originalimage.Width / originalimage.Height * width
                End If

                thumbimage = New Bitmap(newwidth, newheight)

                Dim gr As Graphics = Graphics.FromImage(thumbimage)
                gr.DrawImage(originalimage, 0, 0, newwidth, newheight)
                thumbimage.Save(context.Server.MapPath("~/Images/stock" + "\" + Path.GetFileName(saveAsPath)), Imaging.ImageFormat.Jpeg)

            Next
        Else
            result = "No File Detected!"

        End If

        context.Response.ContentType = "text/plain"
        context.Response.Write(result)

    End Sub

Open in new window

Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Do you have the same exact folder layout and same exact permissions set on each server?
Avatar of BrianFord

ASKER

Yes I do,

Well, let me give a caviat to that answer, Server B is my site hosted at GoDaddy, I have contacted them about this issue and they have 'assured' me that the folder permissions on the server are fine.

Since posting this issue I have even published the site to 3 other IIS server and it works perfectly, it's just the GoDaddy server that doesn't and they won't help me troubleshoot any further as they says everything on their end is fine. :(
I would double check on your own to ensure that the folder is writable to the .net user https://www.godaddy.com/help/set-directory-permissions-windows-6481
Thanks Scott,

I tried your suggestion but I don't have the 'FTP File Manager' option.

Also I'm not actually using FTP to upload the file so not sure if that would make ay difference?
That is the file manager in Godaddy where you set the permissions.  

SET DIRECTORY PERMISSIONS (WINDOWS)

You can set different directory permissions for each of the directories in your Windows® Hosting account, besides the root directory. This lets you control who can access your files and which actions they can perform on files in those directories.

1) Log in to your Account Manager.

2)Click Web Hosting.

3)Next to the hosting account you want to use, click Manage.

4)From the Tools section, click FTP File Manager.

5)Select the directory for which you'd like to set permissions.

6)Click Privacy.

7)Select the permissions you want to use:
-Web visible — Directory contents are visible to users.
-Web writable — Applications can write to this directory.
-Inherit — Inherit permissions from parent directory. You must deselect this option if you want to select permissions on a per-directory basis.
-Set all subfolders to inherit permissions — Changes all subfolders to inherit permissions from this folder.

8)Click OK.
Thanks Scott,

I think I need to contact GoDaddy support again as I simply don't see any place on my management  console to manage the file/folder permissions, even following your post above I simply don't have the option.

I do have a 'File Manager' link but not an 'FTP File Manager' link.

when I go to the File Manager link I only get the option to add remove files or folders but not manage permissions.

Maybe it's becaue it's a sared server and not dedicated.
I don't use godaddy. But somewhere you should be able to set the permissions and it is possible the instructions are wrong because they have different hosting plans that may have different instructions. There was not much to go on here so your best bet is to contact them and ask the method to use for setting folder permissions.
ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'll give that a try, thank you
close