237 lines
8.2 KiB
C#
237 lines
8.2 KiB
C#
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Diagnostics;
|
||
using System.IO;
|
||
using System.Linq;
|
||
using System.Web;
|
||
using VRS.Util;
|
||
|
||
namespace VRS.Handler
|
||
{
|
||
/// <summary>
|
||
/// CosFiles 的摘要说明
|
||
/// </summary>
|
||
public class CosFiles : BaseHandler, IHttpHandler
|
||
{
|
||
|
||
public void ProcessRequest(HttpContext context)
|
||
{
|
||
if (HasFile(context))
|
||
{
|
||
var file = UploadDefaultFile(context);
|
||
var extension = file.FileName.Substring(file.FileName.LastIndexOf('.'));
|
||
//var ext = ".data";
|
||
var ext = extension;
|
||
if (file.ContentLength > 0)
|
||
{
|
||
byte[] buffer = new byte[5 * 1024 * 1024];
|
||
int actual = 0;
|
||
Stopwatch sw1 = new Stopwatch();
|
||
//开始计时
|
||
sw1.Start();
|
||
//先保存到内存流中MemoryStream
|
||
MemoryStream ms = new MemoryStream();
|
||
while ((actual = file.InputStream.Read(buffer, 0, 5 * 1024 * 1024)) > 0)
|
||
{
|
||
ms.Write(buffer, 0, actual);
|
||
}
|
||
ms.Position = 0;
|
||
//再从内存流中读取到byte数组中
|
||
buffer = ms.ToArray();
|
||
|
||
sw1.Stop();
|
||
//获取运行时间[毫秒]
|
||
var ElapsedMilliseconds1 = sw1.ElapsedMilliseconds;
|
||
BasePage.write_log("字节转化毫米:" + ElapsedMilliseconds1);
|
||
|
||
var type = "upload";
|
||
string appid = "1300932214";
|
||
string region = "ap-nanjing"; //地域
|
||
string buket_name = "lzy";//lzy
|
||
var originalName = file.FileName;
|
||
var index = originalName.LastIndexOf(".");
|
||
var extend = originalName.Substring(index + 1);
|
||
CosClient client = new CosClient(appid, region);
|
||
string file_name = BasePage.GetRoomNo() + "." + extend;
|
||
|
||
Stopwatch sw3 = new Stopwatch();
|
||
//开始计时
|
||
sw3.Start();
|
||
|
||
var src = client.UploadFile(buket_name, "/" + type + "/" + file_name, buffer);
|
||
|
||
sw3.Stop();
|
||
//获取运行时间[毫秒]
|
||
var times3 = sw3.ElapsedMilliseconds;
|
||
BasePage.write_log("上传cos毫秒:" + times3);
|
||
|
||
if (src.Code == 200)
|
||
{
|
||
var download_url = "https://" + buket_name + "-" + appid + ".cos." + region + ".myqcloud.com/" + type + "/" + file_name;
|
||
var result = GetResult(true, download_url, "上传成功!");
|
||
context.Response.Write(result);
|
||
context.Response.End();
|
||
}
|
||
else
|
||
{
|
||
var result = GetResult(false, null, "上传失败,原因:" + src.Message);
|
||
context.Response.Write(result);
|
||
context.Response.End();
|
||
}
|
||
}
|
||
}
|
||
var end = GetResult(false, null, "文件不存在");
|
||
context.Response.Write(end);
|
||
context.Response.End();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 异步完成上传COS
|
||
/// </summary>
|
||
/// <param name="obj"></param>
|
||
void UploadAsync(IAsyncResult obj)
|
||
{
|
||
PostedFile postedFile = (PostedFile)obj.AsyncState;
|
||
try
|
||
{
|
||
/*
|
||
CosClass2 cosClass = new CosClass2(ConfigurationManager.AppSettings["Protocol"], ConfigurationManager.AppSettings["Bucket"], ConfigurationManager.AppSettings["Region"], ConfigurationManager.AppSettings["SecretId"], ConfigurationManager.AppSettings["SecretKey"]);
|
||
postedFile.url = cosClass.Upload(string.Format("/{0}", postedFile.fileName), postedFile.postArray);
|
||
*/
|
||
var type = "upload";
|
||
string appid = "1300932214";
|
||
string region = "ap-nanjing"; //地域
|
||
string buket_name = "lzy";//lzy
|
||
CosClient client = new CosClient(appid, region);
|
||
Stopwatch sw3 = new Stopwatch();
|
||
//开始计时
|
||
sw3.Start();
|
||
long times3 = 0;
|
||
|
||
|
||
var originalName = postedFile.fileName;
|
||
var index = originalName.LastIndexOf(".");
|
||
var prefix = originalName.Substring(0, index);
|
||
var extend = originalName.Substring(index+1);
|
||
var bytes = postedFile.postArray;
|
||
string file_name = BasePage.GetRoomNo() +"."+ extend;
|
||
|
||
var src = client.UploadFile(buket_name, "/" + type + "/" + file_name, bytes);
|
||
//结束计时
|
||
sw3.Stop();
|
||
if (src.Code == 200)
|
||
{
|
||
var download_url = "https://" + buket_name + "-" + appid + ".cos." + region + ".myqcloud.com/" + type + "/" + file_name;
|
||
postedFile.state = 1;
|
||
postedFile.url = download_url;
|
||
}
|
||
else
|
||
{
|
||
postedFile.state = 2;
|
||
postedFile.message = "上传失败,原因:" + src.Message;
|
||
}
|
||
}
|
||
catch(Exception ex)
|
||
{
|
||
postedFile.state = 2;
|
||
postedFile.message = "上传失败,原因:" + ex.Message;
|
||
}
|
||
}
|
||
public class PostedFile
|
||
{
|
||
public string fileName;
|
||
public byte[] postArray;
|
||
public string url;
|
||
public int state;
|
||
/// <summary>
|
||
/// 消息
|
||
/// </summary>
|
||
public string message;
|
||
}
|
||
|
||
public bool IsReusable
|
||
{
|
||
get
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取GUID
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
protected string GetGuid()
|
||
{
|
||
return Guid.NewGuid().ToString();
|
||
}
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 是否有上传文件
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
protected bool HasFile(HttpContext context)
|
||
{
|
||
return context.Request.Files.Count > 0;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 上传文件集合
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
protected HttpFileCollection UploadFiles(HttpContext context)
|
||
{
|
||
if (context.Request.Files.Count > 0)
|
||
return context.Request.Files;
|
||
else
|
||
return null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取上传默认文件(第1个文件)
|
||
/// </summary>
|
||
/// <returns>如果不存在则返回null</returns>
|
||
protected HttpPostedFile UploadDefaultFile(HttpContext context)
|
||
{
|
||
var collection = UploadFiles(context);
|
||
if (null == collection)
|
||
return null;
|
||
else
|
||
return collection[0];
|
||
}
|
||
/// <summary>
|
||
/// 返回上传文件集合
|
||
/// </summary>
|
||
/// <returns>如果不存在则返回null</returns>
|
||
protected HttpFileCollection UpLoadFileCollection(HttpContext context)
|
||
{
|
||
var collection = UploadFiles(context);
|
||
if (null == collection)
|
||
return null;
|
||
else
|
||
return collection;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取上传默认文件(第1个文件)
|
||
/// </summary>
|
||
/// <returns>如果不存在则返回null</returns>
|
||
protected HttpPostedFile UploadDefaultFile(HttpContext context,out string fileName)
|
||
{
|
||
fileName = "";
|
||
var defaultFile = UploadDefaultFile(context);
|
||
if (null == defaultFile)
|
||
{
|
||
return null;
|
||
}
|
||
else
|
||
{
|
||
fileName = defaultFile.FileName;
|
||
return defaultFile;
|
||
}
|
||
}
|
||
}
|
||
} |