|
|
@@ -5,6 +5,7 @@ using System.Web.UI.HtmlControls;
|
|
5
|
5
|
using System.Drawing;
|
|
6
|
6
|
using System.Net;
|
|
7
|
7
|
using System.Text.RegularExpressions;
|
|
|
8
|
+using System.IO.Compression;
|
|
8
|
9
|
|
|
9
|
10
|
namespace CallCenter.Utility
|
|
10
|
11
|
{
|
|
|
@@ -452,11 +453,26 @@ namespace CallCenter.Utility
|
|
452
|
453
|
/// <summary>
|
|
453
|
454
|
/// 检查上传的文件的类型,是否允许上传。
|
|
454
|
455
|
/// </summary>
|
|
455
|
|
- private bool IsUploadMp(string Ext)
|
|
|
456
|
+ private bool IsUploadMp3(string Ext)
|
|
456
|
457
|
{
|
|
457
|
458
|
Ext = Ext.Replace(".", "");
|
|
458
|
459
|
bool b = false;
|
|
459
|
|
- string[] arrFileType = ("mp3;mp4").Split(';');
|
|
|
460
|
+ string[] arrFileType = ("mp3").Split(';');
|
|
|
461
|
+ foreach (string str in arrFileType)
|
|
|
462
|
+ {
|
|
|
463
|
+ if (str.ToLower() == Ext.ToLower())
|
|
|
464
|
+ {
|
|
|
465
|
+ b = true;
|
|
|
466
|
+ break;
|
|
|
467
|
+ }
|
|
|
468
|
+ }
|
|
|
469
|
+ return b;
|
|
|
470
|
+ }
|
|
|
471
|
+ private bool IsUploadMp4(string Ext)
|
|
|
472
|
+ {
|
|
|
473
|
+ Ext = Ext.Replace(".", "");
|
|
|
474
|
+ bool b = false;
|
|
|
475
|
+ string[] arrFileType = ("mp4").Split(';');
|
|
460
|
476
|
foreach (string str in arrFileType)
|
|
461
|
477
|
{
|
|
462
|
478
|
if (str.ToLower() == Ext.ToLower())
|
|
|
@@ -710,7 +726,7 @@ namespace CallCenter.Utility
|
|
710
|
726
|
}
|
|
711
|
727
|
|
|
712
|
728
|
/// <summary>
|
|
713
|
|
- /// 图片转Base64
|
|
|
729
|
+ /// 文件转Base64
|
|
714
|
730
|
/// </summary>
|
|
715
|
731
|
/// <param name="ImageFileName">图片的完整路径</param>
|
|
716
|
732
|
/// <returns></returns>
|
|
|
@@ -718,50 +734,91 @@ namespace CallCenter.Utility
|
|
718
|
734
|
{
|
|
719
|
735
|
try
|
|
720
|
736
|
{
|
|
721
|
|
- var img = UrlToImage(ImageFileName);
|
|
722
|
|
- Bitmap bmp = new Bitmap(img);
|
|
723
|
737
|
|
|
724
|
|
- MemoryStream ms = new MemoryStream();
|
|
725
|
|
- bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
|
|
726
|
|
- byte[] arr = new byte[ms.Length];
|
|
727
|
|
- ms.Position = 0;
|
|
728
|
|
- ms.Read(arr, 0, (int)ms.Length);
|
|
729
|
|
- ms.Close();
|
|
730
|
|
- return Convert.ToBase64String(arr);
|
|
|
738
|
+ WebClient mywebclient = new WebClient();
|
|
|
739
|
+ byte[] Bytes = mywebclient.DownloadData(ImageFileName);
|
|
|
740
|
+ return Convert.ToBase64String(Bytes);
|
|
731
|
741
|
}
|
|
732
|
742
|
catch (Exception ex)
|
|
733
|
743
|
{
|
|
734
|
744
|
return null;
|
|
735
|
745
|
}
|
|
736
|
746
|
}
|
|
|
747
|
+ public bool IsImg(string ext)
|
|
|
748
|
+ {
|
|
|
749
|
+ return IsUpload(ext);
|
|
|
750
|
+ }
|
|
|
751
|
+ public bool IsMp3(string ext)
|
|
|
752
|
+ {
|
|
|
753
|
+ return IsUploadMp3(ext);
|
|
|
754
|
+ }
|
|
|
755
|
+ public bool IsMp4(string ext)
|
|
|
756
|
+ {
|
|
|
757
|
+ return IsUploadMp4(ext);
|
|
|
758
|
+ }
|
|
|
759
|
+
|
|
|
760
|
+
|
|
|
761
|
+ #endregion
|
|
|
762
|
+ #region
|
|
|
763
|
+ public string CompressString(string str)
|
|
|
764
|
+ {
|
|
|
765
|
+ string compressString = "";
|
|
|
766
|
+ byte[] compressBeforeByte = System.Text.Encoding.GetEncoding("UTF-8").GetBytes(str);
|
|
|
767
|
+ byte[] compressAfterByte = Compress(compressBeforeByte);
|
|
|
768
|
+ compressString = Convert.ToBase64String(compressAfterByte);
|
|
|
769
|
+ return compressString;
|
|
|
770
|
+ }
|
|
737
|
771
|
/// <summary>
|
|
738
|
|
- /// URI图片转Base64
|
|
|
772
|
+ /// 压缩
|
|
739
|
773
|
/// </summary>
|
|
740
|
|
- /// <param name="url"></param>
|
|
|
774
|
+ /// <param name="data"></param>
|
|
741
|
775
|
/// <returns></returns>
|
|
742
|
|
- public Image UrlToImage(string url)
|
|
|
776
|
+ public byte[] Compress(byte[] data)
|
|
743
|
777
|
{
|
|
744
|
|
- WebClient mywebclient = new WebClient();
|
|
745
|
|
- byte[] Bytes = mywebclient.DownloadData(url);
|
|
746
|
|
- using (MemoryStream ms = new MemoryStream(Bytes))
|
|
|
778
|
+ try
|
|
747
|
779
|
{
|
|
748
|
|
- Image outputImg = Image.FromStream(ms);
|
|
749
|
|
- return outputImg;
|
|
|
780
|
+ MemoryStream ms = new MemoryStream();
|
|
|
781
|
+ GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true);
|
|
|
782
|
+ zip.Write(data,0, data.Length);
|
|
|
783
|
+ zip.Close();
|
|
|
784
|
+ byte[] buffer = new byte[ms.Length];
|
|
|
785
|
+ ms.Position =0 ;
|
|
|
786
|
+ ms.Read(buffer,0 , buffer.Length);
|
|
|
787
|
+ ms.Close();
|
|
|
788
|
+ return buffer;
|
|
|
789
|
+
|
|
|
790
|
+ }
|
|
|
791
|
+ catch (Exception e)
|
|
|
792
|
+ {
|
|
|
793
|
+ throw new Exception(e.Message);
|
|
750
|
794
|
}
|
|
751
|
795
|
}
|
|
752
|
|
- public bool IsImg(string ext)
|
|
753
|
|
- {
|
|
754
|
|
- return IsUpload(ext);
|
|
755
|
|
- }
|
|
756
|
|
- public bool IsMp(string ext)
|
|
|
796
|
+ #endregion
|
|
|
797
|
+
|
|
|
798
|
+ #region url下载文件并保存本地
|
|
|
799
|
+ public string downloadurl(string url,string filename)
|
|
757
|
800
|
{
|
|
758
|
|
- return IsUploadMp(ext);
|
|
759
|
|
- }
|
|
|
801
|
+ try
|
|
|
802
|
+ {
|
|
|
803
|
+ WebClient mywebclient = new WebClient();
|
|
|
804
|
+ byte[] Bytes = mywebclient.DownloadData(url);
|
|
760
|
805
|
|
|
|
806
|
+ if (!Directory.Exists(_SavePath)) Directory.CreateDirectory(_SavePath);
|
|
761
|
807
|
|
|
|
808
|
+ using (var fs = new FileStream(_SavePath+ filename, FileMode.Create))
|
|
|
809
|
+ {
|
|
|
810
|
+ fs.Write(Bytes, 0, Bytes.Length);
|
|
|
811
|
+ fs.Flush();
|
|
|
812
|
+ return "";
|
|
|
813
|
+ }
|
|
|
814
|
+ }
|
|
|
815
|
+ catch (Exception ex)
|
|
|
816
|
+ {
|
|
|
817
|
+ return ex.Message;
|
|
|
818
|
+ }
|
|
|
819
|
+ }
|
|
762
|
820
|
#endregion
|
|
763
|
821
|
|
|
764
|
|
-
|
|
765
|
822
|
#region base64转音视频文件
|
|
766
|
823
|
/// <summary>
|
|
767
|
824
|
/// Base64转文件
|
|
|
@@ -770,15 +827,21 @@ namespace CallCenter.Utility
|
|
770
|
827
|
/// <returns></returns>
|
|
771
|
828
|
public bool Base64ToMP(string strbase,string filepath)//path中需要包含文件名
|
|
772
|
829
|
{
|
|
773
|
|
- string outPath = filepath;
|
|
774
|
|
- var contents = Convert.FromBase64String(strbase);
|
|
775
|
|
- using (var fs = new FileStream(outPath, FileMode.Create, FileAccess.Write))
|
|
|
830
|
+ try
|
|
776
|
831
|
{
|
|
777
|
|
- fs.Write(contents, 0, contents.Length);
|
|
778
|
|
- fs.Flush();
|
|
779
|
|
- return true;
|
|
|
832
|
+ string outPath = filepath;
|
|
|
833
|
+ var contents = Convert.FromBase64String(strbase);
|
|
|
834
|
+ using (var fs = new FileStream(outPath, FileMode.Create, FileAccess.Write))
|
|
|
835
|
+ {
|
|
|
836
|
+ fs.Write(contents, 0, contents.Length);
|
|
|
837
|
+ fs.Flush();
|
|
|
838
|
+ return true;
|
|
|
839
|
+ }
|
|
|
840
|
+ }
|
|
|
841
|
+ catch(Exception ex)
|
|
|
842
|
+ {
|
|
|
843
|
+ return false;
|
|
780
|
844
|
}
|
|
781
|
|
-
|
|
782
|
845
|
}
|
|
783
|
846
|
#endregion
|
|
784
|
847
|
}
|