1

C# ASP.Net と Visual Studio 2012 Ultimate を使用。

フォームの一部のコードを再利用しました。ftp サーバーから画像をダウンロードします。

public class FTPdownloader
{
    public Image Download(string fileName, string ftpServerIP, string ftpUserID, string ftpPassword)
    {
        FtpWebRequest reqFTP;
        Image tmpImage = null;
        try
        {
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));
            reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
            reqFTP.UseBinary = true;
            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

            FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
            Stream ftpStream = response.GetResponseStream();

            tmpImage = Image.FromStream(ftpStream);

            ftpStream.Close();
            //outputStream.Close();
            response.Close();
        }
        catch (Exception ex)
        {
            //MessageBox.Show(ex.Message);
        }
        return tmpImage;
    }
}

うまく機能し、フォームでこのように呼び出すだけです。

imgPart.Image = ftpclass.Download("" + "" + ".jpg", "address/images", "user", "pass");

今、これはwinformsに最適です。私の新しいプロジェクトは、asp.net Web フォームです。同じことをするためにそれが必要です。私はこのコードを再利用しましたが、問題ないようですが、メソッドを img.Image に呼び出すと、img.Image が asp.net に存在しないことがわかりました。基本的には画像を返しますが、私が見つけることができる最も近いものは、もちろん文字列である Img.ImageUrl です。

だから、これがこのコードへのわずかな変更であることを願っています。

どんな助けでも素晴らしいでしょう。みんなありがとう!

4

1 に答える 1

2

System.Drawing.Imageダウンロード関数によって返されるものとSystem.Web.UI.Webcontrols.ImageASP.NETのイメージコントロール()の間に競合があります。

FTPダウンロード機能を少し変更して、ファイルをダウンロードして保存し、ImageWebコントロールで使用できるようにすることで問題を単純化できます。

ダウンロード機能を次のように変更します。

private void Download(string fileName, string ftpServerIP, string ftpUserID, string ftpPassword, string outputName)
{
    using(WebClient request = new WebClient())
    {
        request.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
        byte[] fileData = request.DownloadData(string.Format("ftp://{0}{1}", ftpServerIP, filename));

        using(FileStream file = File.Create(Server.MapPath(outputName)))
        {
            file.Write(fileData, 0, fileData.Length);
        }
    }
}

このコードを使用して画像を取得できます。

// Download image
ftpclass.Download("/images/myimage.jpg", "server-address", "user", "pass", "/mysavedimage.jpg");

// Now link to the image
imgPart.ImageUrl = "/mysavedimage.jpg";

お役に立てれば。

于 2013-01-18T10:39:44.357 に答える