本文介绍了Web表单将上载文件失败到存储服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的系统中有一个上传功能,允许用户上传他们的pdf文件。 当我从我的电脑上传文件到服务器时,没有问题 我的电脑 - > //192.168.2.5/web/file/< ----我的系统源代码服务器 我将上传目的地更改为其他服务器后(//192.10 .1.1 / Intranet /),无法上传。 防火墙已在服务器192.168.2.5和192.10.1.1之间打开 我的上传功能 受保护 void btn_add( object sender,EventArgs e) { string title = title_p.Text; int loggedUserID = Convert.ToInt32(会话[ loggedUserID]); List< BOL.UserInfo> userslist = new UserInfos()。List(); BOL.UserInfo loggeduser = userslist.Where(x = > x.UserID == loggedUserID).FirstOrDefault(); if (FileUploadControl.HasFile) { String ext = System.IO.Path.GetExtension(FileUploadControl.FileName); if (ext.ToLower()== .pdf) { string path1 = \\\\\\ 192.168.1.10 \\Intranet \\; if (FileUploadControl.HasFile) { int fileSize = FileUploadControl.PostedFile.ContentLength; // int maxFileSize = 1024; if (FileUploadControl.PostedFile.ContentLength > 5242880 ) { ClientScript.RegisterStartupScript(GetType(), alert, alert(' + 文件太大 + ');,真); return ; } 其他 { string filename = Path.GetFileName(FileUploadControl.FileName); 尝试 { if (System.IO.File。存在(Server.MapPath(path1)+ filename)) { filename =(Path.GetFileNameWithoutExtension(FileUploadControl.FileName)+ DateTime.Now.ToString( yyyy-MM-dd HHmmtt)+ Path.GetExtension(FileUploadControl.FileName)); FileUploadControl.SaveAs(Server.MapPath(path1)+ filename); DAL.leadp insertdata = new leadp(); // training_insert.add_request(loggeduser.SUBSIDIARY_CD,Main_CD,SUB_CD,Justification,loggeduser.EmployeeNo,loggeduser。 CostCenter,exp_datee.ToString(),e_name,loca,filename,path1 + filename); insertdata.lead_insert(loggeduser.SUBSIDIARY_CD,title,loggeduser.EmployeeNo,filename,path1 + filename); string message = 已提交; ClientScript.RegisterStartupScript(GetType(), alert, alert(' + message + ');, true ); // Response.Redirect(erequest_list.aspx); } else { filename = Path.GetFileName(FileUploadControl.FileName); FileUploadControl.SaveAs(Server.MapPath(path1)+ filename); DAL.leadp insertdata = new leadp(); insertdata.lead_insert(loggeduser.SUBSIDIARY_CD,title,loggeduser.EmployeeNo,filename,path1 + filename); string message = 已提交; ClientScript.RegisterStartupScript(GetType(), alert, alert(' + message + ');, true ); } } catch (例外情况) { // StatusLabel.Text =上传状态:无法上传文件。发生以下错误:+ ex.Message; } } } } 其他 { ClientScript.RegisterStartupScript(GetType(), alert, alert(' + 只能上传pdf文件 + ');,真); } } else { ClientScript.RegisterStartupScript(GetType(), alert, alert(' + 请插入pdf文件 + ');, true ); } } 我尝试过的事情: asaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 解决方案 您以前的位置是否有效因为上传的图像文件存储在您的网站根文件夹中,该文件夹具有在IIS中配置的适当权限。 您上传的新路径是在您的网站之外托管的,这就是为什么您正在收到错误(例如权限错误)。要解决这个问题,您需要设置一个虚拟目录(具有您想要的真实目录),并在IIS中确保没有对该文件夹的直接访问(特别注意读取和目录列表权限)。 PS:作为一般规则,工作的原则是属于网站的内容应该保留在wwwroot文件夹结构中,就像(例如)属于的内容一样C:\ Windows \ System32不应该进入我的文档。 i have a upload function in my system which allow user to upload their pdf file.when i upload file from my pc to server , no issuemy pc --> //192.168.2.5/web/file/ <---- my system source code serverafter i change the upload destination to other server( //192.10.1.1/Intranet/ ),it fail to upload.firewall has been open between server 192.168.2.5 and 192.10.1.1below are my upload functionprotected void btn_add(object sender, EventArgs e) { string title = title_p.Text; int loggedUserID = Convert.ToInt32(Session["loggedUserID"]); List<BOL.UserInfo> userslist = new UserInfos().List(); BOL.UserInfo loggeduser = userslist.Where(x => x.UserID == loggedUserID).FirstOrDefault(); if (FileUploadControl.HasFile) { String ext = System.IO.Path.GetExtension(FileUploadControl.FileName); if (ext.ToLower() == ".pdf" ) { string path1 = "\\\\192.168.1.10\\Intranet\\"; if (FileUploadControl.HasFile) { int fileSize = FileUploadControl.PostedFile.ContentLength; //int maxFileSize = 1024; if (FileUploadControl.PostedFile.ContentLength > 5242880) { ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + "File size too big" + "');", true); return; } else { string filename = Path.GetFileName(FileUploadControl.FileName); try { if (System.IO.File.Exists(Server.MapPath(path1) + filename)) { filename = (Path.GetFileNameWithoutExtension(FileUploadControl.FileName) + DateTime.Now.ToString("yyyy-MM-dd HHmmtt") + Path.GetExtension(FileUploadControl.FileName)); FileUploadControl.SaveAs(Server.MapPath(path1) + filename); DAL.leadp insertdata = new leadp(); //training_insert.add_request(loggeduser.SUBSIDIARY_CD, Main_CD, SUB_CD, Justification, loggeduser.EmployeeNo, loggeduser.CostCenter, exp_datee.ToString(), e_name, loca, filename, path1+filename); insertdata.lead_insert(loggeduser.SUBSIDIARY_CD, title,loggeduser.EmployeeNo, filename, path1 + filename); string message = "Submitted"; ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true); //Response.Redirect("erequest_list.aspx"); } else { filename = Path.GetFileName(FileUploadControl.FileName); FileUploadControl.SaveAs(Server.MapPath(path1) + filename); DAL.leadp insertdata = new leadp(); insertdata.lead_insert(loggeduser.SUBSIDIARY_CD, title, loggeduser.EmployeeNo, filename, path1 + filename); string message = "Submitted"; ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + message + "');", true); } } catch (Exception ex) { //StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; } } } } else { ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + "Only can upload pdf file" + "');", true); } } else { ClientScript.RegisterStartupScript(GetType(), "alert", "alert('" + "Please insert pdf file" + "');", true); } }What I have tried:asaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 解决方案 Your previous location will work because the uploaded image files are stored within your website root folder which has proper permissions configured in IIS.Your new path for upload is hosted outside of your website which why you are getting error (e.g permissions error). To overcome that, you would need to setup a virtual directory (with the real directory where you want it) and in IIS ensure that there is no direct access to that folder (paying particular attention to read and directory listing permissions).PS: As a general rule, work on the principle that what belongs to the website should stay within the wwwroot folder structure, just as (for example) what belongs in "C:\Windows\System32" shouldn't go in "My Documents". 这篇关于Web表单将上载文件失败到存储服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-13 03:23