Exemplo n.º 1
0
        /// <summary>
        /// 上传附件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static async Task <PathValue> Upload(FileStream fileStream, string fileName, string type)
        {
            HttpContent httpContent = new StreamContent(fileStream);

            httpContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data");
            string httpResult = "";

            using (MultipartFormDataContent mulContent = new MultipartFormDataContent("----WebKitFormBoundaryrXRBKlhEeCbfHIY"))
            {
                mulContent.Add(httpContent, "file", fileName);
                string url = GetHttpAddress() + controller;
                httpResult = await HttpHelper.PostHttpClient(url, mulContent);
            }
            //上传成功后删除本地文件
            dynamic result = JsonHelper.DeserializeObject(httpResult);

            if (result.code == "500")
            {
                throw new ServiceException("webapi请求错误:" + result.message);
            }
            PathValue pathValue = new PathValue();

            pathValue.DatePath = result.datepath;

            pathValue.FilePath = string.Format("http://{0}/{1}/{2}", GetConfigurationSection("webapi").GetSection("HttpAddresss").Value, type, pathValue.DatePath);//nginx路由匹配
            pathValue.FileName = fileName;
            return(pathValue);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 上传附件
        /// </summary>
        /// <param name="urlPaths">本地图片相对路径</param>
        /// <returns></returns>
        public static IList <PathValue> Upload(IList <string> urlPaths)
        {
            IList <PathValue> pathValues = new List <PathValue>();

            for (int i = 0; i < urlPaths.Count; i++)
            {
                string fileName = urlPaths[i].Substring(urlPaths[i].LastIndexOf("/") + 1);
                try
                {
                    PathValue pathValue = Upload(GetFilePath(urlPaths[i]), fileName).GetAwaiter().GetResult();
                    pathValues.Add(pathValue);
                }
                catch (Exception)
                {
                    continue;
                }
            }
            return(pathValues);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 保存文件到本地,返回图片路径
        /// </summary>
        /// <param name="fileNameWithExtension"></param>
        /// <returns></returns>
        public static PathValue SaveFile(string fileNameWithExtension)
        {
            int      index       = fileNameWithExtension.LastIndexOf('.');
            string   extension   = fileNameWithExtension.Substring(index, fileNameWithExtension.Length - index); //获取后缀名
            string   webpath     = ConstantKey.WebRoot;                                                          //网站根路径
            string   guid        = Guid.NewGuid().ToString().Replace("-", "");                                   //生成guid
            string   newFileName = guid + extension;
            DateTime dateTime    = DateTime.Now;
            string   datePath    = string.Format("/{0}/{1}/{2}/", dateTime.Year, dateTime.Month, dateTime.Day); //路径日期部分
            string   fullPath    = string.Format("{0}/TempFile{1}", webpath, datePath);                         //全路径

            DirectoryHelper.CreateDirectory(fullPath);                                                          //创建目录
            PathValue pathValue = new PathValue();

            pathValue.DatePath = datePath;
            pathValue.FileName = newFileName;
            pathValue.FilePath = fullPath + newFileName;
            return(pathValue);
        }