在FTP文件夹上载图像时出错 [英] Error in Uploading image on FTP Folder

查看:96
本文介绍了在FTP文件夹上载图像时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想在ftp文件夹中上传一些图片。



我执行了它和它在localhost中正常工作...但现在我上传了该程序在网上,它没有工作!它有这个错误:



无法找到路径的一部分'C:\ Upload \ image.jpg'



你对此有什么想法吗?



- - - - - - - - - - - - - - - - - - - - - - - - -

Hi,

I want to upload some images in the ftp folder.

I performed it and it worked properly in localhost... but now I uploaded the program on the web and it did not work!!! It has this error:

Could not find a part of the path 'C:\Upload\imagename.jpg'

Would you have any idea about this?

- - - - - - - - - - - - - - - - - - - - - - - - -

<form id="form1" runat="server">

<asp:FileUpload ID="imgFileUpload" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="FTP Upload" />
<br />
<asp:Image id="Image1" runat="server" AlternateText="Image text" ImageAlign="left"/>
</form>



- - - - - - - - - - - - - - - - - - - - - - - - -


- - - - - - - - - - - - - - - - - - - - - - - - -

private bool UploadToFTP(string strFTPFilePath, string strLocalFilePath, string strUserName, string strPassword)
{
try
{
//Create a FTP Request Object and Specfiy a Complete Path
FtpWebRequest reqObj = (FtpWebRequest)WebRequest.Create(strFTPFilePath);

//Call A FileUpload Method of FTP Request Object
reqObj.Method = WebRequestMethods.Ftp.UploadFile;

//If you want to access Resourse Protected,give UserName and PWD
reqObj.Credentials = new NetworkCredential(strUserName, strPassword);

// Copy the contents of the file to the byte array.
byte[] fileContents = File.ReadAllBytes(strLocalFilePath);
reqObj.ContentLength = fileContents.Length;

//Upload File to FTPServer
Stream requestStream = reqObj.GetRequestStream();
requestStream.Write(fileContents, 0, fileContents.Length);
requestStream.Close();
FtpWebResponse response = (FtpWebResponse)reqObj.GetResponse();
response.Close();
}

catch (Exception Ex)
{
throw Ex;
}
return true;
}

- - - - - - - - - - - - - - - - - - - - - - - - -

protected void Button1_Click(object sender, EventArgs e)

{
if (imgFileUpload.HasFile)
{
string FileName=imgFileUpload.PostedFile.FileName;

imgFileUpload.SaveAs("C:\\Upload\\" + FileName);
string filePath = "ftp://www.ariavid.com/Upload/";
filePath += FileName;
string fileLocation = "C:\\Upload\\" + FileName;
UploadToFTP(filePath, fileLocation, "username", "password");

Image1.ImageUrl = "ftp://username:password@www.ariavid.com/Upload/" + FileName;
}
}

- - - - - - - - - - - - - - - - - - - - - - - - -



提前致谢...



最好的问候,



Shery


Thanks in advance...

Best Regards,

Shery

推荐答案

问题与FTP无关。找不到路径,就是这样。



首先,没有硬编码路径名有用的情况。应始终在运行时根据用户输入,环境和/或某些配置文件计算文件路径。在您的情况下,不清楚为什么要在本地保存文件。如果这是ASP.NET应用程序并且文件已经上传到您的站点,那么您已经从HTTP请求获得了其内容。



此外,您应该了解您的ASP.NET页面/应用程序无法访问HTTP服务器主机的文件系统中的任何任意文件。 Web应用程序通常在沙盒环境中执行,其中由于重要的安全原因,对主机资源的访问受到限制。特别是,您只能访问为站点配置的根目录下的文件系统对象。另见:

http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath%28v=vs.110%29.aspx [ ^ ]。



-SA
The problem has nothing to do with FTP. The path is not found, that's all.

First of all, there are no cases when a hard-coded path name can be useful. The file paths should always be calculated during runtime based on user input, environment and/or some configuration files. In your case, it's not clear why would you save the file locally at all. If this is the ASP.NET application and the file was already uploaded to your site, you already has its content from HTTP request.

Also, you should understand that your ASP.NET page/application cannot access any arbitrary file in the file system of the HTTP server's host computer. The Web applications are normally executed in a sandboxed environment where the access to the host resources is limited, for important security reasons. In particular, you can only access the file system objects under the root directory configured for your site. See also:
http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath%28v=vs.110%29.aspx[^].

—SA




我将其更改为此代码,但它仍然无法正常运行并出现错误:



无法找到文件'C:\Program Files(x86)\ Common Files \ Mysrosoft Shared \\ \\ DevServer\10.0\System.Web.HttpInputStream'。



我的代码在这里:



Hi,
I changed it to this code, but still it did not work and has an error:

Could not find file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\System.Web.HttpInputStream'.

my code here:

private bool UploadToFTP(string strFTPFilePath, string strLocalFilePath, string strUserName, string strPassword)
        {
            try
            {
                //Create a FTP Request Object and Specfiy a Complete Path
                System.Net.FtpWebRequest reqObj = (FtpWebRequest) WebRequest.Create(strFTPFilePath);

                //Call A FileUpload Method of FTP Request Object
                reqObj.Method = WebRequestMethods.Ftp.UploadFile;

                //If you want to access Resourse Protected,give UserName and PWD
                reqObj.Credentials = new NetworkCredential(strUserName, strPassword);

                // Copy the contents of the file to the byte array.
                FileStream fStream = File.OpenRead(strLocalFilePath);
                byte[] fileContents = new byte[fStream.Length];
                fStream.Read(fileContents, 0, (int) fStream.Length);
                reqObj.ContentLength = fileContents.Length;

                //Upload File to FTPServer
                Stream requestStream = reqObj.GetRequestStream();
                requestStream.Write(fileContents, 0, fileContents.Length);
                requestStream.Close();
                FtpWebResponse response = (FtpWebResponse) reqObj.GetResponse();
                response.Close();
            }

            catch (Exception Ex)
            {
                //throw Ex;
                Label1.Text = Ex.ToString();
            }
            return true;
    }

        
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (imgFileUpload.HasFile)
            {
                string FileName = imgFileUpload.PostedFile.FileName;
                string filePath = "ftp://www.ariavid.com/Upload/";
                filePath += FileName;
                UploadToFTP(filePath, imgFileUpload.PostedFile.InputStream.ToString(), "username", "password");

                Image1.ImageUrl = "ftp://username:password@www.ariavid.com/Upload/" + FileName;
            }
        }



最诚挚的问候,

shery


Best Regards,
shery


这篇关于在FTP文件夹上载图像时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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