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

BrianFordAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Do you have the same exact folder layout and same exact permissions set on each server?
0
BrianFordAuthor Commented:
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. :(
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

BrianFordAuthor Commented:
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?
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
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.
0
BrianFordAuthor Commented:
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.
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
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.
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I saw a similar post with a reply from godaddy

This article only applies to our Classic Web Hosting. If you are using a newer hosting plan with cPanel or Plesk, check the steps in these articles:
https://www.godaddy.com/help/set-permissions-16067 - cPanel
https://www.godaddy.com/help/set-directory-permissions-16135 - Plesk


You are probably using plesk,  

SET DIRECTORY PERMISSIONS

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.

Log in to your Account Manager.

Click Web Hosting.

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

Go to the Files tab.

Navigate to the file or directory whose permissions you want to change.

Mouse-over the file or directory, and then to the right, click the downward-facing arrow, and then select Change Permissions.

Select the permissions you want to use, and then click OK.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BrianFordAuthor Commented:
I'll give that a try, thank you
0
BrianFordAuthor Commented:
close
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Swift Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.