文件上传

            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 });
            }
View Code

 

posted @ 2017-08-25 14:11  连先森  阅读(89)  评论(0编辑  收藏  举报