1.需在共享目录的服务器上加入一个有权限(所有权限,包括读、写、删除等权限)的账号名
2.MVC站点webconfig文件中,<system.web>节点中加入配置节点,
<identity impersonate="true" userName="域\账号" password="口令"/>
3.在保存时,则可以直接进行保存
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
/// <summary>
/// 保存文件,将上传的文件写入至共享目录内
/// </summary>
/// <param name="file"></param>
/// <param name="saveAsFullName"></param>
/// <returns></returns>
protected PageActionReturn SaveUploadFile(HttpPostedFile file, string saveAsFullName)
{
PageActionReturn ret = new PageActionReturn();
try
{
if (string.IsNullOrEmpty(file.FileName))
{
ret.IsOK = false;
ret.Message = "无指定文件";
}
else
{
System.IO.FileInfo fileInfo = new System.IO.FileInfo(saveAsFullName);
System.IO.DirectoryInfo folder = fileInfo.Directory;
CreatePath(folder);
file.SaveAs(saveAsFullName);
ret.IsOK = true;
ret.ReturnData = saveAsFullName;
}
}
catch (Exception ex)
{
ret.IsOK = false;
ret.Message = ex.Message;
ret.StackTrace = ex.StackTrace;
}
return ret;
}
/// <summary>
/// 建立目录
/// 如目录不存在,则一直创建,前提是拥有根目录权限
/// </summary>
/// <param name="folder"></param>
private void CreatePath(System.IO.DirectoryInfo folder)
{
if (!folder.Exists)
{
if (!folder.Parent.Exists)
{CreatePath(folder.Parent);}
folder.Create();
}
}