- Home /
Upload a zip file to a web server iOS
Folks,
I've created a .zip archive of images, sounds and an SQL file. I need to upload it to a web server from which the user can later retrieve the .zip to install on another iOS device.
How would I go about this?
I know it will involve form.AddBinaryData, but just have no clue where to start.
I've seen many examples, but none that seem to exactly address my needs as most just deal with uploading .PNG.
Also: Is it necessary to have a .PHP file to receive the upload or can I just send it directly to a folder on the server?
Here is the code I've been using, but it always ends in a 405 error and no file is sent to the server. Do I need a php/asp page to receive the file?
using UnityEngine;
using System.Collections;
public class UploadFile : MonoBehaviour {
string mainUrl = "http://www.logomagik.com/upload/";
string saveLocation;
void Start() {
saveLocation = GetDocumentPath.GetiPhoneDocumentsPath() + "/";
StartCoroutine("PrepareFile");
}
IEnumerator PrepareFile() {
Debug.Log("saveLoacation = " + saveLocation);
//Read the zip file
WWW loadTheZip = new WWW("file:///"+saveLocation+"myTestZip.zip");
yield return loadTheZip;
StepTwo (loadTheZip);
}
void StepTwo (WWW post) {
StartCoroutine("UpoadTheZip",post);
}
IEnumerator UpoadTheZip (WWW post) {
//Create a form to send, and add zip converted to byte array
WWWForm form = new WWWForm();
form.AddBinaryData("myTestZip",post.bytes,"myTestZip.zip","application/zip");
//Send POST request
string url = mainUrl;
WWW POSTZIP = new WWW(url,form);
Debug.Log("Sending zip...");
yield return POSTZIP;
Debug.Log("Zip sent!");
Debug.Log("Obtained: "+POSTZIP.text);
}
}
ty, /Julian
Answer by TinyDev · Jul 15, 2013 at 04:37 AM
To your specific question "Do I need a php/asp page to receive the file?" Yes, since you are using HTTP to send the file you will need to send to an HTTP server. A PHP/ASP page would be one way to receive the file - you need something in the request pipeline of the server to handle your request.
Edit: I've included some minimalistic code (below) an aspx page...copy paste and name file uploadFile.aspx (if your website is IIS...otherwise you could write something similar in PHP or a JSP or whatever). Using the ASPX page, change your url to http://{yourwebsite}/uploadFile.aspx (i.e. rather than http://{yourwebsite}/upload - that would work if you created a RESTful site with a template of /upload but that's a bit of work and a quick and dirty ASPX page will do the trick).
Your ZIP file will be retrieved from the POSTed request, and saved to a directory called {upload} off the root of your web root.
Change the directory as you see fit (I was just matching the URL you had).
The code below just sends a nominal response if successful and the exception message if not.
You will need to set the permissions on your web server to allow writing to whatever directory you specify. In IIS world that means giving the domain account for the app pool permission or specifically specifying ACLs for the directory. Depending on how the site is hosted that might be easy (e.g. GoDaddy using IIS and File Manager set permissions), on a hosted IIS site use IIS admin, and so on.
<%@ Import Namespace="System"%>
<%@ Import Namespace="System.IO"%>
<%@ Import Namespace="System.Net"%>
<%@ Import NameSpace="System.Web"%>
<Script language="C#" runat=server>
void Page_Load(object sender, EventArgs e)
{
foreach (string f in Request.Files.AllKeys)
{
HttpPostedFile file = Request.Files[f];
string fqn = String.Format(Server.MapPath("\\upload\\{0}"), System.IO.Path.GetFileName(file.FileName));
try
{
file.SaveAs(fqn);
Response.Write("all good");
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
}
</Script>
<html>
<body>
</body>
</html>
Your answer
![](https://koobas.hobune.stream/wayback/20220613105725im_/https://answers.unity.com/themes/thub/images/avi.jpg)