上传文件--媒体文件+获取上传文件的属性信息 layui的upload控件

上传文件--媒体文件+获取上传文件的属性信息--使用layui的upload

c# .net core 6.0 MVC控制器

.Net Core解除文件上传大小限制

1、

【.Net Core】获取绝对路径、相对路径 

注入:

using IHostingEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;

private readonly IHostingEnvironment _hostingEnvironment;

public FileController(IHostingEnvironment hostingEnvironment)
{
    _hostingEnvironment = hostingEnvironment;
}
注入IHostingEnvironment

 

2、后台控制器获取上传文件(示例为:MP3)完整信息保存

--获取文件详细信息

方式一:通过引用shell32.dll文件获取

方式二:通过字节截取方式获取(只能获取个别信息)

从MP3中提取歌曲信息(C#)

一首MP3的额外信息存放在文件的最后面,共占128个字节:

一首MP3的额外信息存放在文件的最后面,共占128个字节,其中包括以下的内容(我们定义一个结构说明):

       public struct Mp3Info

        {
            public string identify;//TAG,三个字节

            public string Title;//歌曲名,30个字节

            public string Artist;//歌手名,30个字节

            public string Album;//所属唱片,30个字节

            public string Year;//年,4个字符

            public string Comment;//注释,28个字节

 

            public char reserved1;//保留位,一个字节

            public char reserved2;//保留位,一个字节

            public char reserved3;//保留位,一个字节

        }

 

       所以,我们只要把MP3文件的最后128个字节分段读出来并保存到该结构里就可以了。函数定义如下:

              /// <summary>

        /// 获取MP3文件最后128个字节

        /// </summary>

        /// <param name="FileName">文件名</param>

        /// <returns>返回字节数组</returns>

        private byte[] getLast128(string FileName)

        {
            FileStream fs = new FileStream(FileName,FileMode.Open,FileAccess.Read);

            Stream stream = fs;

 

            stream.Seek(-128,SeekOrigin.End);

 

            const int seekPos = 128;

            int rl = 0;

            byte[] Info = new byte[seekPos];

            rl = stream.Read(Info,0,seekPos);

 

            fs.Close();

            stream.Close();

 

            return Info;

        }

 

       再对上面返回的字节数组分段取出,并保存到Mp3Info结构中返回。

              /// <summary>

        /// 获取MP3歌曲的相关信息

        /// </summary>

        /// <param name = "Info">从MP3文件中截取的二进制信息</param>

        /// <returns>返回一个Mp3Info结构</returns>

        private Mp3Info getMp3Info(byte[] Info)

        {
            Mp3Info mp3Info = new Mp3Info();

 

            string str = null;

            int i;

            int position = 0;//循环的起始值

            int currentIndex = 0;//Info的当前索引值

            //获取TAG标识

            for(i = currentIndex;i<currentIndex+3;i++)

            {
                str = str+(char)Info;

   

                position++;

            }

            currentIndex = position;

            mp3Info.identify = str;

 

            //获取歌名

            str = null;

            byte[] bytTitle = new byte[30];//将歌名部分读到一个单独的数组中

            int j = 0;

            for(i = currentIndex;i<currentIndex+30;i++)

            {
                bytTitle[j] = Info;   

                position++;

                j++;

            }

            currentIndex = position;

            mp3Info.Title = this.byteToString(bytTitle);

 

            //获取歌手名

            str = null;

            j = 0;

            byte[] bytArtist = new byte[30];//将歌手名部分读到一个单独的数组中

            for(i = currentIndex;i<currentIndex+30;i++)

            {
                bytArtist[j] = Info;   

                position++;

                j++;

            }

            currentIndex = position;

            mp3Info.Artist = this.byteToString(bytArtist);

 

            //获取唱片名

            str = null;

            j = 0;

            byte[] bytAlbum = new byte[30];//将唱片名部分读到一个单独的数组中

            for(i = currentIndex;i<currentIndex+30;i++)

            {
                bytAlbum[j] = Info;   

                position++;

                j++;

            }

            currentIndex = position;

            mp3Info.Album = this.byteToString(bytAlbum);

 

            //获取年

            str = null;

            j = 0;

            byte[] bytYear = new byte[4];//将年部分读到一个单独的数组中

            for(i = currentIndex;i<currentIndex+4;i++)

            {
                bytYear[j] = Info; 

                position++;

                j++;

            }

            currentIndex = position;

            mp3Info.Year = this.byteToString(bytYear);

           

            //获取注释

            str = null;

            j = 0;

            byte[] bytComment = new byte[28];//将注释部分读到一个单独的数组中

            for(i = currentIndex;i<currentIndex+25;i++)

            {
                bytComment[j] = Info;   

                position++;

                j++;

            }

            currentIndex = position;

            mp3Info.Comment = this.byteToString(bytComment);

 

            //以下获取保留位

            mp3Info.reserved1 = (char)Info[++position];

            mp3Info.reserved2 = (char)Info[++position];

            mp3Info.reserved3 = (char)Info[++position];

 

            return mp3Info;

        }

       上面程序用到下面的方法:

              /// <summary>

        /// 将字节数组转换成字符串

        /// </summary>

        /// <param name = "b">字节数组</param>

        /// <returns>返回转换后的字符串</returns>

        private string byteToString(byte[] b)

        {
            Encoding enc = Encoding.GetEncoding("GB2312");

            string str = enc.GetString(b);

            str = str.Substring(0,str.IndexOf('/0') >= 0 ? str.IndexOf('/0') : str.Length);//去掉无用字符

 

            return str;

        }

 

       改名怎么办呢?我们按(演唱者)歌名 的格式对歌曲进行改名,程序如下:

       /// <summary>

        /// 更改文件名

        /// </summary>

        /// <param name="filePath">文件名</param>

        /// <returns></returns>

        private bool ReName(string filePath)

        {
            if(File.Exists(filePath))

            {
                Mp3Info mp3Info = new Mp3Info();

                mp3Info = this.getMp3Info(this.getLast128(filePath));//读出文件信息

 

                mp3Info.Artist = this.DeleteNotValue(mp3Info.Artist);

                mp3Info.Title = this.DeleteNotValue(mp3Info.Title);

 

                if(mp3Info.Artist.Trim().Length==0)

                {
                    mp3Info.Artist="未命名";

                }

 

                if(mp3Info.Title.Trim().Length==0)

                {
                    mp3Info.Title="未知名歌曲";

                }

 

                try

                {
                    //更名

                    File.Move(filePath,filePath.Substring(0,filePath.ToLower().LastIndexOf("//")).Trim() + "//" + "(" + mp3Info.Artist.Trim() + ")" +mp3Info.Title.Trim() + ".mp3");

                    return true;

                }

                catch(Exception)

                {
                    return false;

                }

            }

            else

            {
                return false;

            }

        }
View Code

 

3、前端使用layui-upload组件布局下载

文件的accept属性取值与MIME的关系_乘风xs的博客-CSDN博客_acceptmime

 

后缀名       MIME名称
*.3gpp    audio/3gpp, video/3gpp
*.ac3    audio/ac3
*.asf       allpication/vnd.ms-asf
*.au           audio/basic
*.css           text/css
*.csv           text/csv
*.doc    application/msword    
*.dot    application/msword    
*.dtd    application/xml-dtd    
*.dwg    image/vnd.dwg    
*.dxf      image/vnd.dxf
*.gif            image/gif    
*.htm    text/html    
*.html    text/html    
*.jp2            image/jp2    
*.jpe       image/jpeg
*.jpeg    image/jpeg
*.jpg          image/jpeg    
*.js       text/javascript, application/javascript    
*.json    application/json    
*.mp2    audio/mpeg, video/mpeg    
*.mp3    audio/mpeg    
*.mp4    audio/mp4, video/mp4    
*.mpeg    video/mpeg    
*.mpg    video/mpeg    
*.mpp    application/vnd.ms-project    
*.ogg    application/ogg, audio/ogg    
*.pdf    application/pdf    
*.png    image/png    
*.pot    application/vnd.ms-powerpoint    
*.pps    application/vnd.ms-powerpoint    
*.ppt    application/vnd.ms-powerpoint    
*.rtf            application/rtf, text/rtf    
*.svf           image/vnd.svf    
*.tif         image/tiff    
*.tiff       image/tiff    
*.txt           text/plain    
*.wdb    application/vnd.ms-works    
*.wps    application/vnd.ms-works    
*.xhtml    application/xhtml+xml    
*.xlc      application/vnd.ms-excel    
*.xlm    application/vnd.ms-excel    
*.xls           application/vnd.ms-excel    
*.xlt      application/vnd.ms-excel    
*.xlw      application/vnd.ms-excel    
*.xml    text/xml, application/xml    
*.zip            aplication/zip    
*.xlsx     application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
*.zip      .zip
View Code

 

 根据完整物理路径获取文件file

   //获取文件
                    FileInfo fileinfo = new FileInfo(fullurl);

 

判断服务器文件存在,C# 判断服务器上文件是否存在(对应文件内是否有该文件)

 https://blog.csdn.net/weixin_33515785/article/details/119284418

var path = " FramePath" ;

var filePath = Server.MapPath(path);

if (System.IO.File.Exists(filePath))

{

var fileName = Path.GetFileName(filePath);

FileInfo info = new FileInfo(filePath);

Response.Clear();

Response.ClearHeaders();

Response.Buffer = false;

Response.ContentType = "application/octet-stream";

Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FrameName + ".zip", System.Text.Encoding.UTF8).Replace("+", "%20"));

Response.AppendHeader("Content-Length", info.Length.ToString());

Response.WriteFile(filePath);

Response.Flush();

Response.End();

return new EmptyResult();

}
View Code

 

C# 重命名文件方法

 C# 重命名文件方法 - enych - 博客园 (cnblogs.com)

 

复制代码
1.                 
       //重命名文件
                        // 改名方法
                        FileInfo fi = new FileInfo("旧路径"); //xx/xx/aa.rar
                        fi.MoveTo("新路径"); //xx/xx/xx.rar



2. 文件流方式
   using (FileStream fsw = new FileStream(path + newName, FileMode.Create, FileAccess.Write))  //打开文件,用于只写  
                        {

                            BinaryWriter bw = new BinaryWriter(fsw); //编写器指向这个文件流  
                                                                     // 遍历文件合并   
                            for (int i = 0; i < chunks; i++)
                            {
                                bw.Write(System.IO.File.ReadAllBytes(path + i.ToString() + "_" + filename));    //打开一个文件读取流信息,将其写入新文件  
                                System.IO.File.Delete(path + i.ToString() + "_" + filename);        //删除指定文件信息  
                                bw.Flush(); //清理缓冲区  
                            }
                        }
View Code

 

C# 移动文件方法

https://www.cnblogs.com/xielong/p/6187308.html (参考文档仅供参考,本人尝试文档中的移动方法没有实现)

 

 //本地移动
                    //获取媒体文件路径
                    string fileurl = res.getfileurl(MGUID, out string fullurl);
                    //获取文件
                    FileInfo fileinfo = new FileInfo(fullurl);
                    //获取放置新目录                       
                    string sBaseDir = getsaveDirPath(FolderID, LoginUserInfo.sUserID);
                    //新文件路径
                    string newpath = sBaseDir +"/"+fileinfo.Name;
                    //移动
                    fileinfo.MoveTo(Path.Combine(fullurl, newpath));
View Code

 

 文件删除

 

 //获取文件
 FileInfo fileinfo = new FileInfo(fullurl);
 fileinfo.Delete(); //删除文件

 

 

解决过的问题:

1、

原因

 2、单引号改为双引号就可

 3、使用layui上传无法显示进度条(暂时未解决显示进度条的问题)

(3条消息) Layui在表格中无法显示进度条(layui-progress)的值_夏已微凉、的博客-CSDN博客

4、layui多文件上传连续两次选择同一文件(第二次选择不触发choose问题)

layui二次上传同一文件 upload组件无反应

问题情况叙述一:

 问题情况叙述二:

只能出现下第二图的情况,下第一个图这种实现不了(同一文件实现不了连续选择)

 问题解决:

    //最重要的
                    uploadInst.config.elem.next()[0].value = '';

 

posted @ 2022-11-12 16:10  じ逐梦  阅读(535)  评论(0编辑  收藏  举报