-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileUp.cs
141 lines (136 loc) · 4.29 KB
/
FileUp.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
namespace CSharpHelper
{
/// <summary>
/// 文件上传类
/// </summary>
public class FileUp
{
public FileUp()
{ }
/// <summary>
/// 转换为字节数组
/// </summary>
/// <param name="filename">文件名</param>
/// <returns>字节数组</returns>
public byte[] GetBinaryFile(string filename)
{
if (File.Exists(filename))
{
FileStream Fsm = null;
try
{
Fsm = File.OpenRead(filename);
return this.ConvertStreamToByteBuffer(Fsm);
}
catch
{
return new byte[0];
}
finally
{
Fsm.Close();
}
}
else
{
return new byte[0];
}
}
/// <summary>
/// 流转化为字节数组
/// </summary>
/// <param name="theStream">流</param>
/// <returns>字节数组</returns>
public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
{
int bi;
MemoryStream tempStream = new System.IO.MemoryStream();
try
{
while ((bi = theStream.ReadByte()) != -1)
{
tempStream.WriteByte(((byte)bi));
}
return tempStream.ToArray();
}
catch
{
return new byte[0];
}
finally
{
tempStream.Close();
}
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="PosPhotoUpload">控件</param>
/// <param name="saveFileName">保存的文件名</param>
/// <param name="imagePath">保存的文件路径</param>
public string FileSc(FileUpload PosPhotoUpload, string saveFileName, string imagePath)
{
string state = "";
if (PosPhotoUpload.HasFile)
{
if (PosPhotoUpload.PostedFile.ContentLength / 1024 < 10240)
{
string MimeType = PosPhotoUpload.PostedFile.ContentType;
if (String.Equals(MimeType, "image/gif") || String.Equals(MimeType, "image/pjpeg"))
{
string extFileString = System.IO.Path.GetExtension(PosPhotoUpload.PostedFile.FileName);
PosPhotoUpload.PostedFile.SaveAs(HttpContext.Current.Server.MapPath(imagePath));
}
else
{
state = "上传文件类型不正确";
}
}
else
{
state = "上传文件不能大于10M";
}
}
else
{
state = "没有上传文件";
}
return state;
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="binData">字节数组</param>
/// <param name="fileName">文件名</param>
/// <param name="fileType">文件类型</param>
//-------------------调用----------------------
//byte[] by = GetBinaryFile("E:\\Hello.txt");
//this.SaveFile(by,"Hello",".txt");
//---------------------------------------------
public void SaveFile(byte[] binData, string fileName, string fileType)
{
FileStream fileStream = null;
MemoryStream m = new MemoryStream(binData);
try
{
string savePath = HttpContext.Current.Server.MapPath("~/File/");
if (!Directory.Exists(savePath))
{
Directory.CreateDirectory(savePath);
}
string File = savePath + fileName + fileType;
fileStream = new FileStream(File, FileMode.Create);
m.WriteTo(fileStream);
}
finally
{
m.Close();
fileStream.Close();
}
}
}
}