文件上传
Posted 连先森
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了文件上传相关的知识,希望对你有一定的参考价值。
string fileName = fileData.FileName; string path = System.Web.HttpContext.Current.Server.MapPath("~/UploadFile"); string filePath = System.IO.Path.Combine(path, fileName); string newFileName=FileHelper.GetUniqueFileName(filePath); string newFilePath = System.IO.Path.Combine(path, newFileName); try { byte[] buf = new byte[fileData.ContentLength]; Stream inputStream = fileData.InputStream; inputStream.Read(buf, 0, fileData.ContentLength); inputStream.Close(); FileStream outputStream = new FileStream(newFilePath, FileMode.Create); outputStream.Write(buf, 0, buf.Length); outputStream.Close(); return Json(new { error = 0, msg = "success", fileName = newFileName, fileUrl = "/UploadFile/" + newFileName }); } catch (Exception e) { return Json(new { error = 1, msg = e.Message }); }
以上是关于文件上传的主要内容,如果未能解决你的问题,请参考以下文章
ajaxFileUpload上传带参数文件及JS验证文件大小