Code-C#:根据URI把资源保存到本地

2021-03-15 03:28

阅读:421

标签:==   失败   本地   VID   text   ===   oat   sage   string   

ylbtech-Code-C#:根据URI把资源保存到本地

 

1.返回顶部
1、
using System;
using System.IO;
using System.Net;

namespace ConsoleApp
{
    class Program
    {
        public const string OCT_STREAM_CONTETNT_TYPE = "application/octet-stream";
        /// 
        /// 临时文件夹
        /// 
        //public readonly static string TempFileFolder = ConfigurationManager.AppSettings["TempFileFolder"];
        public readonly static string TempFileFolder = @"c:\cmis\temp";
        /// 
        /// 附件上载时使用
        /// 
        public readonly static string AttachmentSavedFolder = @"c:\cmis\doc";
        /// 
        /// 默认的数据Provider名称
        /// 
        public readonly static string CurrentProviderName = "CMISDOCDATA";

        static void Main()
        {
            Console.WriteLine("===== 开始下载文件 =====");
            GetDownload(Guid.NewGuid(), "https://images.cnblogs.com/cnblogs_com/storebook/1356555/o_abc.jpg");
            Console.WriteLine("===== 完成下载文件 =====");
            Console.ReadKey();
        }

        #region 根据URI把资源保存到本地
        /// 
        /// 根据URI把资源保存到本地
        /// 
        /// 
        /// 
        public static void GetDownload(Guid id, string uri)
        {
            Directory.CreateDirectory(TempFileFolder);
            string path = string.Empty;
            EnumAttachmentStatus status = EnumAttachmentStatus.Fail;
            string result = string.Empty;

            #region 1.根据uri把文件存到本地
            try
            {
                string filename = id.ToString() + uri.Substring(uri.LastIndexOf(.));
                path = string.Format(@"{0}\{1}", TempFileFolder, filename);
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                Stream responseStream = response.GetResponseStream();
                if (response.ContentType == "text/html")
                {
                    status = EnumAttachmentStatus.Fail;
                    result = "[下载失败]资源无效。";
                }
                else
                {
                    Stream stream = new FileStream(path, FileMode.Create);
                    byte[] bArr = new byte[1024];
                    int size = responseStream.Read(bArr, 0, bArr.Length);
                    while (size > 0)
                    {
                        stream.Write(bArr, 0, size);
                        size = responseStream.Read(bArr, 0, bArr.Length);
                    }
                    stream.Close();
                    responseStream.Close();
                    status = EnumAttachmentStatus.OK;
                    result = "下载成功。";
                }
            }
            catch (Exception e)
            {
                status = EnumAttachmentStatus.Fail;
                result = "[下载失败-异常]" + e.Message;
            }
            #endregion

            string extension = System.IO.Path.GetExtension(path);
            Directory.CreateDirectory(AttachmentSavedFolder);
            string savedFileName = string.Format("{0}_{1}_{2}_{4}{3}"
                       , id
                       , System.IO.Path.GetRandomFileName()
                       , DateTime.Now.ToString("yyyyMMdd_hh_mm")
                       , extension
                       , CurrentProviderName);
            string destFileName = System.IO.Path.Combine(AttachmentSavedFolder, savedFileName);
            #region 2.移动文件
            if (status == EnumAttachmentStatus.OK)
            {
                try
                {
                    File.Move(path, destFileName);
                    status = EnumAttachmentStatus.OK;
                    result = "[Move]文件上传成功。";
                }
                catch (Exception e)
                {
                    status = EnumAttachmentStatus.Fail;
                    result = "[Move-文件上传失败-异常]" + e.Message;
                }
            }
            #endregion

            long filesize = 0;  //    
            string folderName = path.Substring(path.LastIndexOf(\\) + 1);
            string displayFileName = "";

            #region 3.把文件存到AM_AttachmentInfo(CMISDOCDATA)表
            if (status == EnumAttachmentStatus.OK)
            {
                Guid attachmentID = Guid.NewGuid();               
            }
            #endregion

            #region 4. 保存下载记录
            #endregion
        }
        #endregion        
    }
    #region 附件状态
    /// 
    /// 附件状态
    /// 
    public enum EnumAttachmentStatus
    {
        /// 
        /// 未处理
        /// 
        Untreated = 0,
        /// 
        /// 成功
        /// 
        OK = 1,
        /// 
        /// 失败
        /// 
        Fail = 2,
    }
    #endregion
}
2、
2.返回顶部
 
3.返回顶部
 
4.返回顶部
 
5.返回顶部
 
 
6.返回顶部
 
技术图片 作者:ylbtech
出处:http://ylbtech.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

Code-C#:根据URI把资源保存到本地

标签:==   失败   本地   VID   text   ===   oat   sage   string   

原文地址:https://www.cnblogs.com/storebook/p/12442156.html


评论


亲,登录后才可以留言!