Exemplo n.º 1
0
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="url"></param>
        /// <param name="save_path"></param>
        /// <returns></returns>
        public static UpLoadFileResult DownloadFile(string url, string save_path)
        {
            var model = new UpLoadFileResult();
            Action <HttpWebResponse, HttpStatusCode> callback = (res, status) =>
            {
                if (status != HttpStatusCode.OK)
                {
                    throw new Exception($"status:{status}");
                }
                using (var s = res.GetResponseStream())
                {
                    IOHelper.CreatePathIfNotExist(save_path);
                    model.SuccessPreparePath = true;
                    var ext = string.Empty;
                    var sp  = url.Split('.');
                    if (sp.Length > 1)
                    {
                        ext = "." + sp[sp.Length - 1];
                    }
                    if (!ValidateHelper.IsPlumpString(ext))
                    {
                        ext = GetExtByContentType(res.ContentType);
                    }
                    var file_path = Path.Combine(save_path, Com.GetUUID() + ext);
                    using (var fs = new FileStream(file_path, FileMode.Create, FileAccess.Write))
                    {
                        var len = 0;
                        var bs  = new byte[1024];
                        while ((len = s.Read(bs, 0, bs.Length)) > 0)
                        {
                            fs.Write(bs, 0, len);
                        }
                        fs.Flush();

                        var fileinfo = new FileInfo(file_path);
                        model.OriginName    = model.FileName = fileinfo.Name;
                        model.WebPath       = "/";
                        model.FileExtension = fileinfo.Extension;
                        model.DirectoryPath = fileinfo.Directory?.FullName;
                        model.FilePath      = fileinfo.FullName;
                        model.FileSize      = (int)fileinfo.Length;
                        model.Info          = fileinfo.LastWriteTime.ToString();

                        model.SuccessUpload = true;
                    }
                }
            };

            SendCore(url, callback, method: RequestMethodEnum.GET, timeout: TimeSpan.FromSeconds(60));

            return(model);
        }
Exemplo n.º 2
0
        public object UpLoadFile(HttpContext context)
        {
            string data = context.Request["data"];
            MDictionary <string, string> dic = new MDictionary <string, string>();

            if (!string.IsNullOrEmpty(data))
            {
                dic = JsonConvert.DeserializeObject <MDictionary <string, string> >(data);
            }
            string progId = dic["ProgId"];

            HttpFileCollection postedFile = context.Request.Files;

            FileTransferService service = new FileTransferService();

            for (int i = 0; i < postedFile.Count; i++)
            {
                UpLoadFileResult ret = service.UpLoadFile1(postedFile[i], progId);
                return(new { uploaded = 1, fileName = ret.FileName, url = ret.FileName, });
            }

            return(null);
        }