/// <summary> /// 上传视频 /// </summary> /// <param name="fileName">视频的文件名称</param> /// <returns>视频的完整路径</returns> public string UploadVidio(HttpContextBase context, UploadConfig uploadConfig, out string fileName) { fileName = string.Empty; string returnPath = string.Empty; try { HttpFileCollectionBase files = context.Request.Files; if (files == null || files.Count == 0) { return(string.Empty); } HttpPostedFileBase file = files[0]; if (file == null) { return(string.Empty); } byte[] bytes = new byte[file.ContentLength]; file.InputStream.Read(bytes, 0, file.ContentLength); file.InputStream.Close(); string channel = "hoteldomestic"; string content = GetFileContent(channel, "mp4"); string result = UploadPost(content, bytes); string[] infoArray = result.Split(','); if (infoArray.Length <= 1) { return(string.Empty); } var path = infoArray[1]; if (string.IsNullOrWhiteSpace(path)) { return(string.Empty); } string[] pathArray = path.Replace("\"", "").Split(':'); if (path.Length > 1) { fileName = pathArray[1]; if (!string.IsNullOrEmpty(fileName)) { //returnPath = ConfigHelper.GetConfigBean("VisitVideoWS") + fileName; returnPath = fileName; } } } catch (Exception ex) { } return(returnPath); }
/// <summary> /// 上传图片 /// </summary> public List <string> UploadImage(HttpContextBase context, UploadConfig uploadConfig, out string fileName) { var paths = new List <string>(); fileName = string.Empty; try { HttpFileCollectionBase files = context.Request.Files; if (files == null || files.Count == 0) { return(paths); } var file = files[0]; if (file == null || file.ContentLength == 0) { return(paths); } byte[] bytes = new byte[file.ContentLength]; file.InputStream.Read(bytes, 0, file.ContentLength); file.InputStream.Close(); //存在服务器文件夹下 fileName = Path.GetFileNameWithoutExtension(file.FileName) + new Random().Next(100) + Path.GetExtension(file.FileName); string filePath = Path.Combine(ArticlePictureRootPath, fileName); FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read); fs.Write(bytes, 0, bytes.Length); fs.Close(); paths.Add("http://" + WebHost + "/CEA_EDU/DigitalContent/GetArticlePictureByPath?path=" + Server.UrlEncode(fileName)); } catch (Exception ex) { } return(paths); }
private bool CheckFileSize(int size, UploadConfig uploadConfig) { return(size < uploadConfig.SizeLimit); }
private bool CheckFileType(string filename, UploadConfig uploadConfig) { var fileExtension = Path.GetExtension(filename).ToLower(); return(uploadConfig.AllowExtensions.Select(x => x.ToLower()).Contains(fileExtension)); }
/// <summary> /// 新版本的上传视频 /// </summary> /// <param name="fileName">视频的文件名称</param> /// <returns>视频的完整路径</returns> public string UploadVideoNewVersion(HttpContextBase context, UploadConfig uploadConfig, out string fileName) { fileName = string.Empty; string returnPath = string.Empty; try { HttpFileCollectionBase files = context.Request.Files; if (files == null || files.Count == 0) { return(string.Empty); } HttpPostedFileBase file = files[0]; if (file == null) { return(string.Empty); } var bytes = new byte[file.ContentLength]; file.InputStream.Read(bytes, 0, file.ContentLength); file.InputStream.Close(); //var domain = ConfigHelper.GetConfigBean("UploadVideoDomain"); string domain = ""; var token = GetToken(domain); if (string.IsNullOrEmpty(token)) { return(string.Empty); } var channel = "hoteldomestic"; var url = string.Format("http://{0}/video/v1/api/upload?channel={1}&token={2}", domain, channel, token); var request = WebRequest.Create(url) as HttpWebRequest; request.Method = "POST"; request.ContentType = "video/mpeg4"; request.Headers.Add("Crc", GetMd5(bytes)); request.ContentLength = bytes.Length; using (Stream newStream = request.GetRequestStream()) { newStream.Write(bytes, 0, bytes.Length); } var content = string.Empty; var response = request.GetResponse() as HttpWebResponse; if (response.StatusCode == HttpStatusCode.OK) { using (StreamReader sr = new StreamReader(response.GetResponseStream())) { content = sr.ReadToEnd(); } } if (string.IsNullOrEmpty(content)) { return(string.Empty); } var jsonContent = JObject.Parse(content); if (jsonContent != null) { fileName = jsonContent["file_name"].ToString(); returnPath = jsonContent["url"].ToString(); } } catch (Exception ex) { } return(returnPath); }
public void UploadFile(HttpContextBase context, UploadConfig uploadConfig) { UploadResult Result = new UploadResult(); var Request = context.Request; //验证文件类型 var file = Request.Files[uploadConfig.UploadFieldName]; if (!CheckFileType(file.FileName, uploadConfig)) { Result.State = UploadState.TypeNotAllow; WriteResult(context, Result); return; } //验证文件大小 if (!CheckFileSize(file.ContentLength, uploadConfig)) { Result.State = UploadState.SizeLimitExceed; WriteResult(context, Result); return; } try { //根据上传类型,走相应的上传服务 string fileName = string.Empty; string url = string.Empty; switch (Request["action"]) { case "uploadvideo": url = UploadVideoNewVersion(context, uploadConfig, out fileName); //if (ConfigHelper.GetConfigBean("UseNewVersionUploadVideo") == "T") //{ // url = UploadVideoNewVersion(context, out fileName); //} //else //{ // url = UploadVidio(context, out fileName); //} break; case "uploadimage": Result.UrlList = UploadImage(context, uploadConfig, out fileName); break; case "uploadscrawl": Result.UrlList = UploadImage(context, uploadConfig, out fileName); break; } Result.OriginFileName = fileName; Result.Url = url; Result.State = UploadState.Success; } catch (Exception e) { Result.State = UploadState.FileAccessError; Result.ErrorMessage = e.Message; } finally { WriteResult(context, Result); } }