web.config
<!--上传文件设置,UploadPath值必然是处事器ip,内网ip最好--> <add key="UploadPath" value="\\172.21.0.10\File" /> <add key="DownloadPath" value="http://x.x.x.x:80/" /> <add key="UserName" value="ShareUser" /> <add key="Password" value="P@ssw0rd" />
东西要领public static string GetConfigString(string key, string @default = "") { return ConfigurationManager.AppSettings[key] ?? @default; } /// <summary> /// 按照文件名(包括文件扩展名)获取要生存的文件夹名称 /// </summary> public class FileHelper { /// <summary> /// 按照文件名(包括文件扩展名)获取要生存的文件夹名称 /// </summary> /// <param>文件名(包括文件扩展名)</param> public static string GetSaveFolder(string fileName) { var fs = fileName.Split('.'); var ext = fs[fs.Length - 1]; var str = string.Empty; var t = ext.ToLower(); switch (t) { case "jpg": case "jpeg": case "png": case "gif": str = "images"; break; case "mp4": case "mkv": case "rmvb": str = "video"; break; case "apk": case "wgt": str = "app"; break; case "ppt": case "pptx": case "doc": case "docx": case "xls": case "xlsx": case "pdf": str = "file"; break; default: str = "file"; break; } return str; } } /// <summary> /// 记录日志辅佐类 /// </summary> public class WriteHelper { public static void WriteFile(object data) { try { string path = $@"C:\Log\"; var filename = $"Log.txt"; if (!Directory.Exists(path)) Directory.CreateDirectory(path); TextWriter tw = new StreamWriter(Path.Combine(path, filename), true); //true在文件末端添加数据 tw.WriteLine($"----发生时间:{DateTime.Now:yyyy-MM-dd HH:mm:ss}---------------------------------------------------------------------"); tw.WriteLine(data.ToJson()); tw.Close(); } catch (Exception e) { } } }
常量/// <summary> /// 文件上传设置项 /// </summary> public class FileUploadConst { /// <summary> /// 上传地点 /// </summary> public static string UploadPath => ConfigHelper.GetConfigString("UploadPath"); /// <summary> /// 文件会见/下载地点 /// </summary> public static string DownloadPath => ConfigHelper.GetConfigString("DownloadPath"); /// <summary> /// 会见共享目任命户名 /// </summary> public static string UserName => ConfigHelper.GetConfigString("UserName"); /// <summary> /// 会见共享目次暗码 /// </summary> public static string Password => ConfigHelper.GetConfigString("Password"); }
详细上传文件代码/// <summary> /// 上传文件到共享文件夹 /// </summary> [HttpPost, Route("api/Upload/UploadAttachment")] [AllowAnonymous] public ServiceResponse<UploadRespModel> UploadAttachment() { var viewModel = new UploadRespModel(); var code = 200; var msg = "上传失败!"; var path = FileUploadConst.UploadPath; //@"\\172.16.10.130\Resource"; var s = connectState(path, FileUploadConst.UserName, FileUploadConst.Password); if (s) { var filelist = HttpContext.Current.Request.Files; if (filelist.Count > 0) { var file = filelist[0]; var fileName = file.FileName; var blobName = FileHelper.GetSaveFolder(fileName); path = $@"{path}\{blobName}\"; fileName = $"{DateTime.Now:yyyyMMddHHmmss}{fileName}"; //共享文件夹的目次 var theFolder = new DirectoryInfo(path); var remotePath = theFolder.ToString(); Transport(file.InputStream, remotePath, fileName); viewModel.SaveUrl = $"{blobName}/{fileName}"; viewModel.DownloadUrl = PictureHelper.GetFileFullPath(viewModel.SaveUrl); msg = "上传乐成"; } } else { code = CommonConst.Code_OprateError; msg = "链接处事器失败"; } return ServiceResponse<UploadRespModel>.SuccessResponse(msg, viewModel, code); } /// <summary> /// 毗连长途共享文件夹 /// </summary> /// <param>长途共享文件夹的路径</param> /// <param>用户名</param> /// <param>暗码</param> private static bool connectState(string path, string userName, string passWord) { bool Flag = false; Process proc = new Process(); try { proc.StartInfo.FileName = "cmd.exe"; proc.StartInfo.UseShellExecute = false; proc.StartInfo.RedirectStandardInput = true; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.RedirectStandardError = true; proc.StartInfo.CreateNoWindow = true; proc.Start(); string dosLine = "net use " + path + " " + passWord + " /user:" + userName; WriteHelper.WriteFile($"dosLine:{dosLine}"); proc.StandardInput.WriteLine(dosLine); proc.StandardInput.WriteLine("exit"); while (!proc.HasExited) { proc.WaitForExit(1000); } string errormsg = proc.StandardError.ReadToEnd(); proc.StandardError.Close(); WriteHelper.WriteFile($"errormsg:{errormsg}"); if (string.IsNullOrEmpty(errormsg)) { Flag = true; } else { throw new Exception(errormsg); } } catch (Exception ex) { WriteHelper.WriteFile(ex); throw ex; } finally { proc.Close(); proc.Dispose(); } return Flag; } /// <summary> /// 向长途文件夹生存当地内容,可能从长途文件夹下载文件到当地 /// </summary> /// <param>要生存的文件的路径,假如生存文件到共享文件夹,这个路径就是当地文件路径如:@"D:\1.avi"</param> /// <param>生存文件的路径,不含名称及扩展名</param> /// <param>生存文件的名称以及扩展名</param> private static void Transport(Stream inFileStream, string dst, string fileName) { WriteHelper.WriteFile($"目次-Transport:{dst}"); if (!Directory.Exists(dst)) { Directory.CreateDirectory(dst); } dst = dst + fileName; if (!File.Exists(dst)) { WriteHelper.WriteFile($"文件不存在,开始生存"); var outFileStream = new FileStream(dst, FileMode.Create, FileAccess.Write); var buf = new byte[inFileStream.Length]; int byteCount; while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0) { outFileStream.Write(buf, 0, byteCount); } WriteHelper.WriteFile($"生存完成"); inFileStream.Flush(); inFileStream.Close(); outFileStream.Flush(); outFileStream.Close(); } }
以上就是ASP.NET 上传文件到共享文件夹的示例的具体内容,更多关于ASP.NET 上传文件的资料请存眷剧本之家其它相关文章!
您大概感乐趣的文章: