文件未上传 [英] File not uploading

查看:74
本文介绍了文件未上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下代码在本地服务器上运行良好,并且我能够上传文件..

hi ,

the below code works well on local server and i''m able to upload file..

HttpFileCollection uploadFilCol = Request.Files;
                try
                {
                    for (int i = 0; i < uploadFilCol.Count; i++) //uploadFilCol.Count
                    {
                        HttpPostedFile file = uploadFilCol[i];
                        string fileExt = Path.GetExtension(file.FileName).ToLower();
                        string fileName = Path.GetFileName(file.FileName);
                      
                        if (file.ContentLength > 0)
                        {

                            if (fileName != string.Empty)
                            {
                               
                                createdir(ftpaddress + Session["userid"].ToString(), user, pass);
                                try
                                {
                                   
                                    if (fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".png" || fileExt == ".jpeg" || fileExt == ".rar" || fileExt == ".zip" || fileExt == ".7z")
                                    {
                                       
                                        FtpWebRequest uploadRequest = (FtpWebRequest)FtpWebRequest.Create(ftpaddress + Session["userid"].ToString() + @"/" + fileName);
                                        Stream requestStream = null;
                                        FtpWebResponse uploadResponse = null;
                                        try
                                        {
                                            Thread.Sleep(20);
                                            uploadRequest.Credentials = new NetworkCredential(user, pass);
                                            uploadRequest.KeepAlive = false;
                                            uploadRequest.UsePassive = false;
                                            uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;
                                            uploadRequest.UseBinary = true;
                                            uploadRequest.Timeout = 10000000;
                                            byte[] buffer = null;
                                            uploadRequest.Proxy = null;
                                            requestStream = uploadRequest.GetRequestStream();
                                            long bytesToRead = file.ContentLength;
                                            int bytesRead = 0;

                                            while (bytesRead < bytesToRead)
                                            {
                                                buffer = new byte[file.ContentLength];

                                                bytesRead += file.InputStream.Read(buffer, 0, buffer.Length);
                                                if (bytesRead == 0)
                                                    break;

                                                requestStream.Write(buffer, 0, bytesRead);
                                              
                                            }


                                            requestStream.Close();
                                            uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();
                                           
                                        }
                                        catch (UriFormatException ex)
                                        {
                                            lbl_error.Text = ex.Message;
                                        }
                                        catch (IOException ex)
                                        {
                                            lbl_error.Text = ex.Message;
                                        }
                                        catch (WebException ex)
                                        {
                                            lbl_error.Text = ex.Message;
                                        }
                                        finally
                                        {
                                            if (uploadResponse != null)
                                                uploadResponse.Close();
                                         
                                            if (requestStream != null)
                                                requestStream.Close();
                                        }
      
                                        this.ShowMessage("" + fileName + " Uploaded ", i);

                                    }
                                    else
                                    {
                                        this.ShowMessage(" <font color=red> This is not a Picture File</font/>", i);
                                    }
                                    //}
                                }
                                catch (Exception ex)
                                {
                                    this.ShowMessage("<font color=red> " + fileName + " Not Uploaded <font>", i);
                                }
                            }
                           
                        }
                        else
                        {
                            this.ShowMessage("<font color=red> " + fileName + " File size not more than 6MB</font>", i);
                        }
                      
                    }
                   

                }
               
            }
        }
        catch (Exception ex)
        {
            Response.Redirect("default.aspx");
        }


    }



但是在将站点托管在服务器上后,它不起作用..也就是说,文件未上传..而且它也没有返回任何错误..我缺少什么吗?..请帮助我...

谢谢...



But it is not working after the site is hosted on the server.. that is, the file is not uploading.. also it doesn''t returns any error.. Am i missing something?.. Pls help me out...

Thanks...

推荐答案

HI尝试执行以下操作

1.检查ftp主机地址和配置文件以更新服务器详细信息
2. Alos检查凭据,如果没有权限,则文件也不会上传到服务器

HI try to do the followings

1. Check the ftp host address,and config file for updating server details
2. Alos check the credentials , if no permission then also file will not get uploaded to server

ftp.Credentials = new NetworkCredential("userid", "password");
    //userid and password for the ftp server to given



3.在创建FtpWebRequest.Create时,为什么要传递Session ID,请检查并尝试执行以下操作



3. While creating FtpWebRequest.Create why you are passing Session ID please check, and try to do as below

string ftpfullpath = "ftp://" + ftphost + ftpfilepath;
    FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);


我认为许可问题.检查asp.net和网络安全性的权限设置.如果未设置权限文件,则不会保存在服务器中.
I think the problem of permission. check the permission settings for asp.net and network security. if you not set permission file will not save in server.


如果您需要上传4MB以上的文件,可以在wen.config
中使用此代码
if you need to upload more than 4MB you can this code in wen.config

<httpRuntime
 maxRequestLength="4096"



或检查此链接
http://msdn.microsoft.com/en-us/library/aa479405.aspx [ ^ ]



or check this link
http://msdn.microsoft.com/en-us/library/aa479405.aspx[^]


这篇关于文件未上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆