string[] allowedExt = new string[] { }, denyedExt = new string[] { };
string savePath = String.Empty;
long maxSize = 10000;
string type = Request.QueryString["Type"];
if(type!=null&&type!=string.Empty)
type=type.ToLower();
else
type="file";
if (type == "image")
{
GetConfig("image", out allowedExt, out denyedExt, out savePath,out maxSize);
}
if (type == "file")
{
GetConfig("file", out allowedExt, out denyedExt, out savePath, out maxSize);
}
if (type == "flash")
{
GetConfig("flash", out allowedExt, out denyedExt, out savePath, out maxSize);
}
if (savePath == string.Empty||savePath=="")
savePath = "~/UserFiles/";
if(!savePath.EndsWith("/"))savePath+="/";
/*********************************************************************************
byte[] bytes1 = System.Text.Encoding.Default.GetBytes("这是字符串\n\n\n\n");
byte[] bytes2 = new byte[] { 1, 33, 23, 3, 0, 56, 55, 235, 5 };//二进制数
byte[] bytes = new byte[bytes1.Length + bytes2.Length];
//合并二进制流
MemoryStream ms = new MemoryStream(bytes);
ms.Write(bytes1, 0, bytes1.Length);
ms.Write(bytes2, 0, bytes2.Length);
int count = 0, pos = 0;
//开始找四个'\n'
for (int i = 0; i < bytes.Length; i++)
{
if (bytes[i] == (int)'\n')
{
count++;
if (count == 4)
{
pos -= 4;
break;
}
}
}
if (count == 4)
{
//这里,bytes字节数组里从0 到 pos 的位置就是你要的字符串
//从pos + 5 开始到最后,就是你要的二进制
}
**********************************************************************************/
byte[] fileData, formData;
formData = Request.BinaryRead(Request.ContentLength);
string head = String.Empty;
Encoding encoding = Encoding.UTF8;
long pos = 0;
for (long i = 0; i < formData.LongLength; i++)
{
if (formData[i] == (byte)'\r' && formData[i + 1] == (byte)'\n' && formData[i + 2] == (byte)'\r' && formData[i + 3] == (byte)'\n')
{
pos = i;
break;
}
}
if (pos == 0) { Response.End(); return; }
head = encoding.GetString(formData, 0, (int)pos);
fileData = new byte[formData.LongLength - pos - 3];
Array.Copy(formData, pos + 4, fileData, 0, formData.LongLength - pos - 4);
/************************************************************************************************
//传来的表单形式是:
//"-----------------------------7d5fa3820f84\r\nContent-Disposition: form-data; name=\"NewFile\"; filename=\"F:\\Documents\\4(10995).jpg\"\r\nContent-Type: image/pjpeg\r\n\r\n
//后面是文件数据
************************************************************************************************/
head = head.ToLower();
head = head.Remove(0, head.IndexOf("\r\n") + 2);
head = head.Replace("\"", "");
string postFileName = string.Empty;
string fileName;//no path
string fileType, fileExt;
postFileName = head.Substring(0, head.IndexOf("\r\n"));//Content-Disposition: form-data; name=\"NewFile\"; filename=\"F:\\Documents\\4(10995).jpg\"
fileType = head.Remove(0, postFileName.Length + 3);//returns:Content-Type: image/pjpeg
postFileName = postFileName.Substring(postFileName.IndexOf("filename=") + "filename=".Length);//C:\path\name
fileName = Path.GetFileName(postFileName);
fileExt = fileName.Substring(fileName.LastIndexOf(".") + 1);
if (fileData.LongLength > maxSize) {
SendResults(2, ResolveUrl(savePath + fileName), fileName, "Too large");
return;
}
bool isallow=false;
foreach(string ext in denyedExt){
if (ext == fileExt) {
isallow = false;
SendResults(202, ResolveUrl(savePath + fileName), fileName, "forrbiden");
return;
}
}