问题描述
各位高手,我在服务器的IIS上部署了一个网站,通过windowsapi上传文件到网络路径失败,只能上传到本地路径,怎么解决呢?系统环境是windowsserver2008R2standardSP164位操作系统,IIS版本7.0
解决方案
解决方案二:
通过windowsapi上传文件到网络路径放出你的代码来,没听说"上传"功能还需要调用API的
解决方案三:
谢谢回复,代码如下:///<summary>默认文件上传///</summary>publicvoidUploadFile(HttpFileCollectionfiles){stringfileServer=System.Configuration.ConfigurationManager.AppSettings["FileServer"];stringfileServerPath=System.Configuration.ConfigurationManager.AppSettings["FileServerPath"];stringfileServerUser=System.Configuration.ConfigurationManager.AppSettings["FileServerUser"];stringfileServerPWD=System.Configuration.ConfigurationManager.AppSettings["FileServerPWD"];NetResourcenr=newNetResource();nr.dwScope=0;nr.dwType=1;nr.lpRemoteName=@fileServer;longstatus=WindowsApis.WNetAddConnection2A(refnr,fileServerUser,fileServerPWD,0);try{for(inti=0;i<files.Count;i++){fileuploadInfofi=newfileuploadInfo();stringfileURL="";stringreceivefileName=files[i].FileName;//全路径fi.fileName=receivefileName.Substring(receivefileName.LastIndexOf("\")+1);//取全文件名stringreceiveFileType=receivefileName.Substring(receivefileName.LastIndexOf(".")+1);//取后缀名stringyear=DateTime.Now.Year.ToString();stringmoth=DateTime.Now.Month.ToString();stringdate=DateTime.Now.ToString("yyyy-MM-dd");stringfoldPath=@fileServerPath+"\HotlineCaseInfo\"+year+"\"+year+"-"+moth+"\"+date;DirectoryInfodir=newDirectoryInfo(foldPath);if(dir.Exists){DirectoryInfo[]Folds=dir.GetDirectories();intisHaveSaveFlode=0;//标识现在文件夹是否满员intlastFold=0;for(intj=0;j<Folds.Length;j++){inttemplastFold=Convert.ToInt32(Folds[j].Name);//取得文件夹名if(templastFold>lastFold){lastFold=templastFold;}FileInfo[]f1=Folds[j].GetFiles();if(f1.Length<500){//获取所有子文件夹名fileURL=foldPath+"\"+Folds[j].ToString();HttpPostedFilehpf=files[i];stringNewFileName=getFileName(f1);fileURL=fileURL+"\"+NewFileName+"."+receiveFileType;hpf.SaveAs(fileURL);//保存文件fi.fileUrl=fileURL;isHaveSaveFlode=1;break;}}if(isHaveSaveFlode==0)//如果当天文件夹中已满{fileURL=foldPath+"\"+getNext(lastFold);DirectoryInfofolddir=newDirectoryInfo(fileURL);if(!folddir.Exists){folddir.Create();}HttpPostedFilehpf=files[i];fileURL=fileURL+"\001."+receiveFileType;hpf.SaveAs(fileURL);//保存文件fi.fileUrl=fileURL;isHaveSaveFlode=1;}}else{fileURL=foldPath+"\001";dir.Create();dir=newDirectoryInfo(fileURL);dir.Create();HttpPostedFilehpf=files[i];fileURL=fileURL+"\001."+receiveFileType;hpf.SaveAs(fileURL);//保存文件fi.fileUrl=fileURL;}filelist.Add(fi);}}catch(Exceptionex){WindowsApis.WNetCancelConnection2A(@fileServer,1,1);LogNet.LogHelper.PrintLog(LogNet.Loggerlevel.ERROR,"DATA_HotLine_FileUpLoad_FileUpLoad","UploadFile",LogNet.LoggerMark.Business,ex.ToString());throwex;}WindowsApis.WNetCancelConnection2A(@fileServer,1,1);}
解决方案四:
usingSystem;usingSystem.Windows.Forms;usingSystem.Runtime.InteropServices;usingSystem.IO;namespaceNetConnect{publicpartialclassNetWareFileCopy:Form{publicNetWareFileCopy(){InitializeComponent();}privatevoidbtNetCopy_Click(objectsender,EventArgse){NetWorkConnection();}#regionDefineNetWareConnectClasspublicenumERROR_ID{ERROR_SUCCESS=0,//SuccessERROR_BUSY=170,ERROR_MORE_DATA=234,ERROR_NO_BROWSER_SERVERS_FOUND=6118,ERROR_INVALID_LEVEL=124,ERROR_ACCESS_DENIED=5,ERROR_INVALID_PASSWORD=86,ERROR_INVALID_PARAMETER=87,ERROR_BAD_DEV_TYPE=66,ERROR_NOT_ENOUGH_MEMORY=8,ERROR_NETWORK_BUSY=54,ERROR_BAD_NETPATH=53,ERROR_NO_NETWORK=1222,ERROR_INVALID_HANDLE_STATE=1609,ERROR_EXTENDED_ERROR=1208,ERROR_DEVICE_ALREADY_REMEMBERED=1202,ERROR_NO_NET_OR_BAD_PATH=1203,ERROR_SESSION_CREDENTIAL_CONFLICT=1219}publicenumRESOURCE_SCOPE{RESOURCE_CONNECTED=1,RESOURCE_GLOBALNET=2,RESOURCE_REMEMBERED=3,RESOURCE_RECENT=4,RESOURCE_CONTEXT=5}publicenumRESOURCE_TYPE{RESOURCETYPE_ANY=0,RESOURCETYPE_DISK=1,RESOURCETYPE_PRINT=2,RESOURCETYPE_RESERVED=8,}publicenumRESOURCE_USAGE{RESOURCEUSAGE_CONNECTABLE=1,RESOURCEUSAGE_CONTAINER=2,RESOURCEUSAGE_NOLOCALDEVICE=4,RESOURCEUSAGE_SIBLING=8,RESOURCEUSAGE_ATTACHED=16,RESOURCEUSAGE_ALL=(RESOURCEUSAGE_CONNECTABLE|RESOURCEUSAGE_CONTAINER|RESOURCEUSAGE_ATTACHED),}publicenumRESOURCE_DISPLAYTYPE{RESOURCEDISPLAYTYPE_GENERIC=0,RESOURCEDISPLAYTYPE_DOMAIN=1,RESOURCEDISPLAYTYPE_SERVER=2,RESOURCEDISPLAYTYPE_SHARE=3,RESOURCEDISPLAYTYPE_FILE=4,RESOURCEDISPLAYTYPE_GROUP=5,RESOURCEDISPLAYTYPE_NETWORK=6,RESOURCEDISPLAYTYPE_ROOT=7,RESOURCEDISPLAYTYPE_SHAREADMIN=8,RESOURCEDISPLAYTYPE_DIRECTORY=9,RESOURCEDISPLAYTYPE_TREE=10,RESOURCEDISPLAYTYPE_NDSCONTAINER=11}[StructLayout(LayoutKind.Sequential)]publicstructNETRESOURCE{publicRESOURCE_SCOPEdwScope;publicRESOURCE_TYPEdwType;publicRESOURCE_DISPLAYTYPEdwDisplayType;publicRESOURCE_USAGEdwUsage;[MarshalAs(UnmanagedType.LPStr)]publicstringlpLocalName;[MarshalAs(UnmanagedType.LPStr)]publicstringlpRemoteName;[MarshalAs(UnmanagedType.LPStr)]publicstringlpComment;[MarshalAs(UnmanagedType.LPStr)]publicstringlpProvider;}publicclassNetworkConnection{[DllImport("mpr.dll")]publicstaticexternintWNetAddConnection2A(NETRESOURCE[]lpNetResource,stringlpPassword,stringlpUserName,intdwFlags);[DllImport("mpr.dll")]publicstaticexternintWNetCancelConnection2A(stringsharename,intdwFlags,intfForce);publicstaticintConnect(stringremotePath,stringlocalPath,stringusername,stringpassword){NETRESOURCE[]share_driver=newNETRESOURCE[1];share_driver[0].dwScope=RESOURCE_SCOPE.RESOURCE_GLOBALNET;share_driver[0].dwType=RESOURCE_TYPE.RESOURCETYPE_DISK;share_driver[0].dwDisplayType=RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_SHARE;share_driver[0].dwUsage=RESOURCE_USAGE.RESOURCEUSAGE_CONNECTABLE;share_driver[0].lpLocalName=localPath;share_driver[0].lpRemoteName=remotePath;Disconnect(localPath);intret=WNetAddConnection2A(share_driver,password,username,1);returnret;}publicstaticintDisconnect(stringlocalpath){returnWNetCancelConnection2A(localpath,1,1);}}#endregion//调用:publicstaticvoidNetWorkConnection(){stringlocalpath="Z:";stringserverPath=@"\10.0.0.1FileShared";stringloginUser="userid";stringloginPassword="password";intstatus=NetworkConnection.Connect(serverPath,localpath,loginUser,loginPassword);if(status==(int)ERROR_ID.ERROR_SUCCESS){//连接成功,在服务器端创建一文件,并写入一字串,FileStreamfs=newFileStream(localpath+@"//ConnectSuccess.txt",FileMode.OpenOrCreate);using(StreamWriterstream=newStreamWriter(fs)){stream.WriteLine("网络连接成功");stream.Flush();stream.Close();}fs.Close();}else{//连接失败Console.WriteLine(status);}//断开连接NetworkConnection.Disconnect(localpath);}}}
解决方案五:
实现文件上传功能#region实现文件上传的功能protectedvoidImgUpFile_Click(objectsender,ImageClickEventArgse){if(this.FileUpload1.PostedFile.FileName!=""){UpFile();//执行上传文件SFUPC();}else{Response.Write("<scriptlanguage='javascript'>alert('对不起,上传文件为空,请选择上传文件!');location=Default.aspx</script>");}}#region该方法用于执行文件上传操作privatevoidUpFile(){//获取文件夹路径stringFilePath=Server.MapPath("./")+"File";//获取客户端上载文件的集合HttpFileCollectionHFC=Request.Files;for(inti=0;i<HFC.Count;i++){//访问指定的文件HttpPostedFileUserHPF=HFC[i];try{//判断文件是否为空if(UserHPF.ContentLength>0){//将上传的文件存储在指定目录下UserHPF.SaveAs(FilePath+"\"+System.IO.Path.GetFileName(UserHPF.FileName));}}catch{LblMessage.Text="上传失败!";}}if(Session["FilesControls"]!=null){Session.Remove("FilesControls");}LblMessage.Text="上传成功!";}