Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by Flamacore · Nov 23, 2017 at 04:25 PM · uploadasyncasynchronousftpwebclient

Unity, WebClient, Upload, Async and Threads.

Hi there everyone. This is rather a question for .NET experts.


OS: Irrelevant, editor script. But Editor works on Windows 10 for this case.
Unity version: 2017.1
IDE: MonoDevelop
Purpose: An editor window script which selects a folder, then compresses it and uploads it to a server via FTP. Upload is the part in question.


State:
I am currently using WebClient.UploadDataAsync to upload a file using FTP credentials. I have two problems which actually come down to one arch-problem. Upon intense research for weeks, I've understood one thing: There's simply no way of checking progress properly and no way of closing the FTP connection once it's opened even if it hangs for a reason which is quite a problem since I have no way of canceling the upload. Getting file size works. Static Coroutine also works. OnFileUploadCompleted fires but only if upload is really completed. OnUploadProgressChanged never fires; Tried zillions of alternatives. Disposed fires but ftp connection does not get closed. CancelAsync() also does nothing.


What happens:
When client.UploadDataAsync(uri, "STOR", bytes); triggers, file(data) upload starts peacefully, getting the size works properly and everything is fine. But after a time, upload just hangs. Simply hangs. No exception throw, no event trigger, nothing. Just stays there forever. I even check my server for FTP connections and it shows active connection with session named UL. Also Getting file size shows total size correct, uploaded size correct also. I check from FileZilla to see and the file is created correctly, named correctly and uploads to a certain part too. Unfortunately that hang occurs at random...


Question:
1. What could I do in order to track progress? I've come up with a way of checking file size with FtpWebRequest every 1-2 seconds but even for an editor script for a developer, that feels like an overkill.
2. How can i check if a timeout occurred? Custom webclient timeout setting does not work it seems. Or is there another way to do it? Could that be server related?
3. How can I completely abort,dispose,kill,murder,destroy,annihilate,disintegrate or obliterate the FTP connection created by the WebClient? :) Sending to dark oblivion is fine too. There seems to be literally no way to access it.
4. I definitely know that is not the greatest way to do it but since this will just work in the editor, I assume using the easier and thread-safe way is OK?


Code: (The complete file is too long, just pasting the relevant part. All using statements are correct. All required variables are pre-defined.)

 public static void UploadFile()
     {
         Debug.Log("Path: " + FilePath);
         Debug.Log("Username: " + FTPUserName);
         Debug.Log("Pass: " + FTPPassword);
         Debug.Log("Host: ftp://" + FTPHost);
 
         Uri uri = new Uri("ftp://"+FTPHost + new FileInfo(FilePath).Name);
         client.Credentials = new System.Net.NetworkCredential(FTPUserName, FTPPassword);
         client.UploadDataCompleted += new UploadDataCompletedEventHandler(OnFileUploadCompleted);
         client.UploadProgressChanged += (object sender, UploadProgressChangedEventArgs e) => {Debug.Log(e.ProgressPercentage);};
         client.Disposed += (sender, e) => {Debug.Log("Disposed of uploader client");};
         client.Timeout = 2000;
         byte[] bytes = File.ReadAllBytes (FilePath);
         client.UploadDataAsync(uri, "STOR", bytes);
 
         EditorLogGenerator.GenerateLog ("Upload started");
         shouldCheck = true;
         StaticCoroutine.DoCoroutine (readSize ());
 
         Uri uri2 = new Uri("ftp://"+FTPHost + "version.flamacore");
         client2.Credentials = new System.Net.NetworkCredential(FTPUserName, FTPPassword);
         client2.UploadFileCompleted += new UploadFileCompletedEventHandler(OnFileUploadCompleted2);
         client2.UploadFileAsync(uri2, "STOR", Application.dataPath + "/../TheLauncher/version/version.flamacore");
         EditorLogGenerator.GenerateLog ("Version File Upload Started");
 
         Uri uri3 = new Uri("ftp://"+FTPHost + "v.flamacore");
         client3.Credentials = new System.Net.NetworkCredential(FTPUserName, FTPPassword);
         client3.UploadDataCompleted += new UploadDataCompletedEventHandler(OnFileUploadCompleted3);
         byte[] bytes3 = File.ReadAllBytes (Application.dataPath + "/../TheLauncher/version/v.flamacore");
         client3.UploadDataAsync(uri3, "STOR", bytes3);
         EditorLogGenerator.GenerateLog ("File List Upload Started");
     }
     public static IEnumerator readSize()
     {
         
         yield return new WaitForSeconds (0.50f);
         while (shouldCheck) {
             t1_ = new Thread (() => {
                 var ftpWebRequest = (FtpWebRequest)WebRequest.Create(new Uri ("ftp://" + FTPHost + new FileInfo (FilePath).Name)); //Create FtpWebRequest with given Request Uri.
                 ftpWebRequest.Credentials = new NetworkCredential (FTPUserName, FTPPassword); //Set the Credentials of current FtpWebRequest
                 ftpWebRequest.Method = WebRequestMethods.Ftp.GetFileSize; //Set the Method of FtpWebRequest incase it has a value.
                 long fileSize = ftpWebRequest.GetResponse().ContentLength;;
                 UploadStatus = "Upload Progress: " + GetBytesReadable(fileSize) + "/" + GetBytesReadable(new System.IO.FileInfo (FilePath).Length);
                 Debug.Log (UploadStatus);
             });
             if (!t1_.IsAlive)
                 t1_.Start ();
             EditorLogGenerator.GenerateLog (t1_.Name + "Upload Progress Thread Started");
             yield return new WaitForSeconds (1.5f);
         }
         
     }











Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

120 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Load level async 1 Answer

How to properly call Async Task 0 Answers

FTP Uploading from Pc or Device, permissions, rejected by remote machine. 1 Answer

How to wait for WWW to finish without Coroutine. 0 Answers

Syntax 501 error, How to upload without error to FTP Server 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges