Unity - A计划(永久有效期) 扫二维码继续学习 二维码时效为半小时

(196评价)
价格: 4019.00元
FTP上传文件,直到文件上传结束之前,程序一直假死
wongvin发起了问答2017-12-19
4
回复
1385
浏览

RT,最近因为项目需要用到FTP上传,在手机上上传文件的时候,发现在上传的时候就会假死,一直上传结束才会恢复正常,不知道有什么办法可以解决,还有想问下UNITY还有其他的方式上传文件的吗,谢谢。

使用的上传代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Net;
using System;

public class UploadVideo
{


    /// <summary>
    /// 上传文件
    /// </summary>
    /// <param name="fileinfo">需要上传的文件</param>
    /// <param name="targetDir">目标路径</param>
    /// <param name="hostname">ftp地址</param>
    /// <param name="username">ftp用户名</param>
    /// <param name="password">ftp密码</param>
    public void UploadFile(FileInfo fileinfo, string targetDir, string hostname, string username, string password)
    {
        //1. check target
        string target;
        if (targetDir.Trim() == "")
        {
            return;
        }
        target = Guid.NewGuid().ToString();  //使用临时文件名


        string URI = "FTP://" + hostname + "/" + targetDir + "/" + target;
        ///WebClient webcl = new WebClient();
        FtpWebRequest ftp = GetRequest(URI, username, password);

        //设置FTP命令 设置所要执行的FTP命令,
        //ftp.Method = System.Net.WebRequestMethods.Ftp.ListDirectoryDetails;//假设此处为显示指定路径下的文件列表
        ftp.Method = WebRequestMethods.Ftp.UploadFile;
        //指定文件传输的数据类型
        ftp.UseBinary = true;
        ftp.UsePassive = true;

        //告诉ftp文件大小
        ftp.ContentLength = fileinfo.Length;
        //缓冲大小设置为2KB
        const int BufferSize = 2048;
        byte[] content = new byte[BufferSize - 1 + 1];
        int dataRead;

        //打开一个文件流 (System.IO.FileStream) 去读上传的文件
        using (FileStream fs = fileinfo.OpenRead())
        {
            try
            {
                //把上传的文件写入流
                using (Stream rs = ftp.GetRequestStream())
                {
                    do
                    {
                        //每次读文件流的2KB
                        dataRead = fs.Read(content, 0, BufferSize);
                        rs.Write(content, 0, dataRead);
                    } while (!(dataRead < BufferSize));
                    rs.Close();
                }

            }
            catch (Exception ex) { }
            finally
            {
                fs.Close();
            }

        }

        ftp = null;
        //设置FTP命令
        ftp = GetRequest(URI, username, password);
        ftp.Method = WebRequestMethods.Ftp.Rename; //改名
        ftp.RenameTo = fileinfo.Name;
        try
        {
            ftp.GetResponse();
        }
        catch (Exception ex)
        {
            ftp = GetRequest(URI, username, password);
            ftp.Method = WebRequestMethods.Ftp.DeleteFile; //删除
            ftp.GetResponse();
            throw ex;
        }
        finally
        {
            //fileinfo.Delete();
        }

        // 可以记录一个日志  "上传" + fileinfo.FullName + "上传到" + "FTP://" + hostname + "/" + targetDir + "/" + fileinfo.Name + "成功." );
        ftp = null;

        #region
        /*****
         *FtpWebResponse
         * ****/
        //FtpWebResponse ftpWebResponse = (FtpWebResponse)ftp.GetResponse();
        #endregion
    }

    private static FtpWebRequest GetRequest(string URI, string username, string password)
    {
        //根据服务器信息FtpWebRequest创建类的对象
        FtpWebRequest result = (FtpWebRequest)FtpWebRequest.Create(URI);
        //提供身份验证信息
        result.Credentials = new NetworkCredential(username, password);
        //设置请求完成之后是否保持到FTP服务器的控制连接,默认值为true
        result.KeepAlive = false;
        return result;
    }
}

所有回复
  • siki 2017-12-20

    在上传的时候,开一个单独的线程处理上传的操作就可以了

    • wongvin 2017-12-20

      感谢siki老师的回复!查阅了资料知道是需要多线程处理,可是对于线程这块的内容不是太懂,能够稍微说明一下吗

      (0) 回复
    • siki 2017-12-20

      回复 @ wongvin: 线程可以看看老师讲解的C#编程 第四季

      (0) 回复
    • wongvin 2017-12-20

      回复 @ siki: 好的,谢谢SIKI老师

      (0) 回复
    还有-2条回复,点击查看
    你还没有登录,请先登录注册
发表回复
你还没有登录,请先 登录或 注册!