public static bool IsPdf(HttpPostedFileBase DocumentUpload) { var pdfString = "%PDF-"; var pdfBytes = Encoding.ASCII.GetBytes(pdfString); var len = pdfBytes.Length; int FileLen; System.IO.Stream MyStream; FileLen = DocumentUpload.ContentLength; byte[] buffer = new byte[len]; // Initialize the stream. MyStream = DocumentUpload.InputStream; // Read the file into the byte array. if (FileLen >= len) { MyStream.Read(buffer, 0, len); } else { MyStream.Read(buffer, 0, FileLen); } return(pdfBytes.SequenceEqual(buffer)); }
private void Page_Load(Object sender, EventArgs e) { HttpFileCollection MyFileCollection; HttpPostedFile MyFile; int FileLen; System.IO.Stream MyStream; MyFileCollection = Request.Files; MyFile = MyFileCollection[0]; FileLen = MyFile.ContentLength; byte[] input = new byte[FileLen]; // Initialize the stream. MyStream = MyFile.InputStream; // Read the file into the byte array. MyStream.Read(input, 0, FileLen); // Copy the byte array into a string. for (int Loop1 = 0; Loop1 < FileLen; Loop1++) { MyString = MyString + input[Loop1].ToString(); } }
static void Main() { MyStream ms = new MyStream(); ms.Read(new byte[] { }, 0, 0); ms.Write(new byte[] { }, 0, 0); ms.Flush(); Console.WriteLine("See log.txt for details"); Console.ReadKey(); }
public void AAA(MyStream stream) { byte[] bytes = new byte[512]; int readCount = 1; while (readCount > 0) { FileStream localStream = new FileStream("c:\\test.txt", FileMode.Append, FileAccess.ReadWrite); readCount = stream.Read(bytes, 0, bytes.Length); localStream.Write(bytes, 0, bytes.Length); localStream.Close(); } }
protected void Button1_Click(object sender, EventArgs e) { //Response.AddHeader("Content-Length", HttpPostedFile f = this.FileUpload1.PostedFile; HttpFileCollection MyFileCollection; HttpPostedFile MyFile; int FileLen; System.IO.Stream MyStream; MyFileCollection = Request.Files; MyFile = MyFileCollection[0]; FileLen = MyFile.ContentLength; FileLen = 200; byte[] input = new byte[FileLen]; // Initialize the stream. MyStream = MyFile.InputStream; MyStream.Position = MyStream.Length - 100; // Read the file into the byte array. MyStream.Read(input, 0, FileLen); string MyString = string.Empty; // Copy the byte array into a string. for (int Loop1 = 0; Loop1 < FileLen; Loop1++) { MyString = MyString + input[Loop1].ToString(); } using (StreamWriter sw = new StreamWriter(@"c:\aaaaa.txt")) { sw.Write(MyString); } MyFile.SaveAs(@"c:\bbbbbbb.bbb"); //Response.Write(MyString); }
private void GetSize() { HttpFileCollection MyFileCollection; HttpPostedFile MyFile; long FileLen = 0; Stream MyStream; MyFileCollection = Request.Files; FileLen = Request.InputStream.Length; if (FileLen > 0) { byte[] input = new byte[FileLen]; MyStream = Request.InputStream; MyStream.Position = 0; MyStream.Read(input, 0, (int)FileLen); FileStream fs = null; string token = Request.Params["token"]; string name = Request.Params["name"]; string[] names = name.Split((".").ToCharArray()); string uploadPath = HttpContext.Current.Server.MapPath("UploadImages" + "\\") + token + "." + names[names.Length - 1]; fs = new FileStream(uploadPath, FileMode.OpenOrCreate, FileAccess.Write); fs.Seek(0, SeekOrigin.End); fs.Write(input, 0, (int)FileLen); fs.Position = 0; FileLen = fs.Length; MyStream.Dispose(); MyStream.Close(); fs.Dispose(); fs.Close(); } string value = "{start:" + FileLen + ",success:'true'}"; Response.Clear(); Response.Write(value); Response.End(); //*/ }
public ActionResult leading_in() { HttpPostedFileBase upfile = Request.Files["leadtxt"]; if (upfile == null) { return(Content("fail|您没有选择文件")); } //判断文件大小是否符合要求 //if (upfile.ContentLength >= (5242880)) //{ // return Content("fail|请上传5M以内的文件!"); //} string ext = Path.GetExtension(upfile.FileName);//获得文件扩展名 if (ext != ".txt") { return(Content("fail|请上传有效的txt文件")); } //利用InputStream 属性直接从HttpPostedFile对象读取文本内容 System.IO.Stream MyStream; int FileLen; FileLen = upfile.ContentLength; // 读取文件的 byte[] byte[] bytes = new byte[FileLen]; MyStream = upfile.InputStream; MyStream.Read(bytes, 0, FileLen); string text = Encoding.UTF8.GetString(bytes); string error = ""; int errorcnt = 0; List <SYSIntegralCode> codes = new List <SYSIntegralCode>(); using (StringReader sr = new StringReader(text)) { try { string line; int lineIndex = 0; while ((line = sr.ReadLine()) != null) { lineIndex++; SYSIntegralCode codeModel = new SYSIntegralCode(); if (string.IsNullOrWhiteSpace(line)) { error += "第" + lineIndex + "行导入失败:" + "空行。"; errorcnt++; continue; } string[] codeArray = line.Split(','); if (codeArray.Length < 3) { error += "第" + lineIndex + "行导入失败:" + "参数数量不正确。"; errorcnt++; continue; } codeModel.WaterCode = codeArray[0]; codeModel.IntegralCode = codeArray[2]; codeModel.Dat = DateTime.Now; codeModel.State = "未使用"; if (codeModel.IsHave()) { error += "第" + lineIndex + "行导入失败:" + "已经存在相同的。"; errorcnt++; continue; } codes.Add(codeModel); } } catch (Exception ex) { sr.Close(); DAL.Log.Instance.Write(ex.ToString(), "SYSIntegralCode_leading_in_error"); return(Content("fail|导入出现异常")); } } foreach (var item in codes) { item.InsertAndReturnIdentity(); } return(Content(string.Format("ok|【导入完成,成功{0}条,失败{1}条】{2}。", codes.Count, errorcnt, error))); }
public ActionResult ErrorFile(Models.ErrorFilePage row) { ViewBag.PreviousUrl = TempData["PreviousUrl"] != null ? TempData["PreviousUrl"] : GetPreviousUrl(); bool Supprimer = false; bool Importer = false; if (Request.Form["Action"] != null) { if (Request.Form["Action"].IndexOf("Imp", StringComparison.OrdinalIgnoreCase) >= 0) { Importer = true; } else if (Request.Form["Action"].IndexOf("Sup", StringComparison.OrdinalIgnoreCase) >= 0) { Supprimer = true; } } System.Web.Script.Serialization.JavaScriptSerializer scriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); var Fichier = row.Fichiers.FirstOrDefault(); if (Fichier != null) { byte[] input = null; int FileLen = Fichier.ContentLength; System.IO.Stream MyStream; input = new byte[FileLen]; MyStream = Fichier.InputStream; MyStream.Read(input, 0, FileLen); row.MyEx = scriptSerializer.Deserialize <MyException>(System.Text.Encoding.UTF8.GetString(input)).EEE(); } else if (!string.IsNullOrWhiteSpace(row.Fichier)) { var filename = System.IO.Path.Combine(Server.MapPath("~/App_Data"), row.Fichier + ".ERDUMP"); if (Supprimer) { System.IO.File.Delete(filename); row.MyEx = null; } else { row.MyEx = scriptSerializer.Deserialize <MyException>(System.IO.File.ReadAllText(filename)).EEE(); } } else { row.MyEx = null; } // Except = exception.EEE(); if (Importer && row.MyEx != null) { try { //throw new System.Data.Entity.Validation.DbEntityValidationException(); var sql = new Models.Entities(); sql.CatchMe_Exception.Add(row.MyEx); sql.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException e) { ViewBag.ErrorMessage = CatchException.Dump(e).ToString().Replace("\n", "<br/>"); return(View(row)); } if (!string.IsNullOrWhiteSpace(row.Fichier)) { var filename = System.IO.Path.Combine(Server.MapPath("~/App_Data"), row.Fichier + ".ERDUMP"); System.IO.File.Delete(filename); } return(RedirectToAction("Erreur", new { id = row.MyEx.CodeCatch })); } return(View(row)); }
public override int Read(byte[] buffer, int offset, int count) { AssertInvariants(); Contracts.Check(!_disposed, "Stream already disposed"); return(MyStream.Read(buffer, offset, count)); }
private Dictionary <string, Dictionary <int, string> > readEvidenceFileToDictionary(HttpPostedFile file, DataTable locusSortOrder) { Dictionary <string, Dictionary <int, string> > val = new Dictionary <string, Dictionary <int, string> >(); if (String.IsNullOrEmpty(file.FileName)) { return(val); } else { if (!String.IsNullOrEmpty(file.FileName)) { int FileLen; System.IO.Stream MyStream; try { FileLen = file.ContentLength; byte[] input = new byte[FileLen]; // Initialize the stream. MyStream = file.InputStream; // Read the file into the byte array. MyStream.Read(input, 0, FileLen); StringBuilder strUploadedContent = new StringBuilder(""); strUploadedContent.Append(Encoding.ASCII.GetString(input, 0, FileLen)); string txtOutPut = Server.HtmlEncode(strUploadedContent.ToString()); string[] list = txtOutPut.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (list.Length > 0) { if (list[0].ToUpper().Contains("REPLICATE")) { //replicate data val.Clear(); for (int i = 1; i < list.Length; i++) { list[i] = list[i].Trim(); string[] curAllele = list[i].Split('\t'); //clean curAllele, remove all empty allele if (curAllele.Length >= 2) { StringBuilder thisAllele = new StringBuilder(""); if (curAllele.Length > 2) //no allele { for (int j = 2; j < curAllele.Length; j++) { thisAllele.Append(curAllele[j].ToString()); thisAllele.Append(","); } } //remove last "," if (!String.IsNullOrEmpty(thisAllele.ToString())) { if (thisAllele[thisAllele.Length - 1] == ',') { thisAllele.Remove(thisAllele.Length - 1, 1); } } if (val.ContainsKey(curAllele[0])) { val[curAllele[0]].Add(Convert.ToInt32(curAllele[1].ToString()), thisAllele.ToString()); } else { Dictionary <int, string> newReplicate = new Dictionary <int, string>(); newReplicate.Add(Convert.ToInt32(curAllele[1].ToUpper().ToString()), thisAllele.ToString()); val.Add(curAllele[0], newReplicate); } } } } else { throw new Exception("Wrong Unknown File"); return(null); } } List <string> loci = LocusSortOrder(locusSortOrder); Dictionary <int, string> emptyLoci = new Dictionary <int, string>(); for (int replicate = 1; replicate <= 3; replicate++) { emptyLoci.Add(replicate, string.Empty); } foreach (string locus in loci) { if (!val.ContainsKey(locus.ToUpper())) { val.Add(locus.ToUpper(), emptyLoci); } } return(val); } catch (Exception eFile) { MessageBox.Show("Error reading file: '" + file.FileName + "'. Please make sure you select a file in the correct format."); return(null); } } } return(null); }
private Dictionary <string, string> readKnownFileToDictionary(HttpPostedFile file, DataTable locusSortOrder) { if (!String.IsNullOrEmpty(file.FileName)) { int FileLen; System.IO.Stream MyStream; Dictionary <string, string> val = new Dictionary <string, string>(); try { FileLen = file.ContentLength; byte[] input = new byte[FileLen]; // Initialize the stream. MyStream = file.InputStream; // Read the file into the byte array. MyStream.Read(input, 0, FileLen); StringBuilder strUploadedContent = new StringBuilder(""); strUploadedContent.Append(Encoding.ASCII.GetString(input, 0, FileLen)); string txtOutPut = Server.HtmlEncode(strUploadedContent.ToString()); string[] list = txtOutPut.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); if (list.Length > 0) { if (list[0].ToUpper().Contains("REPLICATE")) { //replicate data throw new Exception("Wrong Suspect1 File"); } else { //suspect1 data for (int i = 1; i < list.Length; i++) { list[i] = list[i].Trim(); string[] curAllele = list[i].Split('\t'); if (curAllele.Length >= 1) { StringBuilder thisAllele = new StringBuilder(""); for (int j = 1; j < curAllele.Length; j++) { thisAllele.Append(curAllele[j].ToString()); thisAllele.Append(","); } if (!String.IsNullOrEmpty(thisAllele.ToString())) { if (thisAllele[thisAllele.Length - 1] == ',') { thisAllele.Remove(thisAllele.Length - 1, 1); } } string locus = curAllele[0].ToUpper(); val.Add(locus, thisAllele.ToString()); } } } } } catch (Exception eFile) { MessageBox.Show("Error reading file: '" + file.FileName + "'. Please make sure you select a file in the correct format."); return(null); } List <string> loci = LocusSortOrder(locusSortOrder); foreach (string locus in loci) { if (!val.ContainsKey(locus.ToUpper())) { val.Add(locus.ToUpper(), string.Empty); } } return(val); } return(null); }