对象存储OSS
作者:朱尚 / 日期:2020-12-11 / 分类:阿里云API接口 / 浏览:2232

用法

/// <summary>
    /// NewsClass.aspx图片上传
    /// </summary>
    /// <param name="context"></param>
    private void NewsClassMFile(HttpContext context)
    {
        string xten = domain.domainname.GetIndexConfigValue("NewsClassFile");
        string FileLength = domain.domainname.GetIndexConfigValue("NewsClassFileLength");
        string _upfilepath = HttpContext.Current.Request["UpFilePath"]; //取得上传的对象名称
        Boolean fileok = false;
        HttpPostedFile fileup = context.Request.Files[_upfilepath];
        if (fileup.ContentLength != 0)
        {
            if (fileup.ContentLength <= Double.Parse(FileLength) * 1024)
            {
                string fileextension = System.IO.Path.GetExtension(fileup.FileName).ToLower();
                string filename1 = DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MM");
                string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + fileextension;//上传的文件名(自定义)+ 文件后缀
                String[] allowedextensions = xten.Split(',');
                for (int i = 0; i < allowedextensions.Length; i++)
                {
                    string lei = allowedextensions[i];
                    if (lei.IndexOf(".") == (-1))
                    {
                        lei = "." + lei;
                    }
                    if (fileextension == lei)
                    {
                        fileok = true;
                        break;
                    }
                }
                if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath("../upload/img/" + filename1)))
                {
                    System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath("../upload/img/" + filename1));
                }
                if (fileok == true)
                {
                    fileup.SaveAs(HttpContext.Current.Server.MapPath("../upload/img/" + filename1 + "/") + "\\" + filename);

                    string Result = "";
                    using (var fs = fileup.InputStream)
                    {
                        //将该文件流保存到OSS中
                        Result = StoreFile("ColumnList/" + filename1 + "/" + filename, fs);
                    }

                    context.Response.Write("{msg: 1, msgbox: \"" + Result + "\"}");
                }
                else
                {
                    context.Response.Write("{msg: 0, msgbox: \"文件类型错误!正确格式为:" + xten + "\"}");
                }
            }
            else
            {
                context.Response.Write("{msg: 0, msgbox: \"文件大小不能超过" + FileLength + "KB!\"}");
            }
        }
        context.Response.End();
    }

引用

/// <summary>
    /// 存储文件到阿里云OSS{文件名/数据流/数据类型}
    /// </summary>
    /// <param name="file_name"></param>
    /// <param name="image_data"></param>
    /// <param name="content_type"></param>
    /// <returns></returns>
    public static string StoreFile(string file_name, Stream fs)
    {
        var client = new OssClient("Your EndPoint", "Your AccessKeyId", "Your AccessKeySecret");
        try
        {
            var meta = new ObjectMetadata();
            meta.ContentType = GetMimeMapping(file_name);
            client.PutObject("Your BucketName", file_name, fs, meta);
            return string.Format("http://file.400301.com/{0}", file_name);
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
    /// <summary>
    /// 获取文件类型
    /// </summary>
    /// <param name="file_name"></param>
    /// <returns></returns>
    public static string GetMimeMapping(string fileName)
    {
        string mimeType = "application/octet-stream";
        string ext = Path.GetExtension(fileName).ToLower();
        RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(ext);
        if (regKey != null && regKey.GetValue("Content Type") != null)
        {
            mimeType = regKey.GetValue("Content Type").ToString();
        }
        return mimeType;
    }
上一篇:视频点播
下一篇:没有了
本文标签: aliyun api OSS
本文链接:http://www.banzhuan.net/detail/360