zoukankan      html  css  js  c++  java
  • 将上传图片文件转成二进制流再存储

     protected void Button1_Click(object sender, EventArgs e)
            {
                byte[] bt = SetImageToByteArray(FileUpload1);//将上传文件转换成byte[]数组
                if (bt != null)
                {
                    string programPath = System.AppDomain.CurrentDomain.BaseDirectory.ToString();//获取当前项目路径
                    string picname = DateTime.Now.ToString("yyyyMMddHHmmss") + new Random().Next(1000, 9999) + ".jpg";
                    string path = programPath +"image/"+ picname;//文件存储路径
                    MemoryStream ms = new MemoryStream(bt);//将byte[]数组存入内存流
                    FileStream fs = new FileStream(path, FileMode.Create);//创建文件流
                    ms.WriteTo(fs);//将内存流写入文件流
                    System.Drawing.Image image = System.Drawing.Image.FromStream(fs, true);//将文件流转成image对象类型
                    image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);//存储对象
                    ms.Close();
                    fs.Close();
                    ms = null;
                    fs = null;
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('ok!')</script>");
                }
            }
            public byte[] SetImageToByteArray(FileUpload FileUpload1)
            {
                string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();//获取文件类型
                if (fileExtension == ".jpg")
                {
                    Stream stream = FileUpload1.PostedFile.InputStream;
                    byte[] photo = new byte[FileUpload1.PostedFile.ContentLength];
                    stream.Read(photo, 0, FileUpload1.PostedFile.ContentLength);
                    stream.Close();
                    return photo;
                }
                else
                { 
                    byte[] bt=null;
                    return bt;
                }
            }
  • 相关阅读:
    Qt状态机实例
    <STL> accumulate 与 自定义数据类型
    <STL> 容器混合使用
    散列表(C版)
    Canonical 要将 Qt 应用带入 Ubuntu
    <STL> set随笔
    C++ 文件流
    视频播放的基本原理
    <STL> pair随笔
    c++ 内存存储 解决char*p, char p[]的问题
  • 原文地址:https://www.cnblogs.com/li-fei/p/3476924.html
Copyright © 2011-2022 走看看