c#Webclient.UploadData在本地工作,但在服务器上失败

时间:2019-01-14 19:16:53

标签: c# model-view-controller ftp upload webclient

我正在尝试部署网站,但是我的其中一个具有图片上传功能的表单出现了错误。

首先,我正在使用NetworkSolutions进行托管,并且其中一个表单具有用于图像的文件上传输入。当我在本地运行应用程序并将映像上传到FTP时,一切正常,但是,当我部署到服务器时,连接似乎超时(因为它挂了片刻),所以我收到消息”对象引用未设置为对象的实例。” 。我应该注意的一件事是未在此服务器上设置SSL,但是我使用的是不安全的端口。

[HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Submit(RegistrationViewModel viewModel)
    {
        if (!ModelState.IsValid)
        {
            return RedirectToAction("Index", viewModel);
        }
        if (Request.Files.Count > 0)
        {

            string path = String.Empty;
            HttpPostedFileBase file = Request.Files[0];

            if (file != null && file.ContentLength > 0)
            {
                string fileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName); // Path.GetFileName(file.FileName);

                try
                {
                    using (WebClient client = new WebClient())
                    {
                        client.Credentials = new NetworkCredential("***", "***");

                        byte[] buffer = new byte[file.ContentLength];
                        file.InputStream.Read(buffer, 0, buffer.Length);
                        file.InputStream.Close();


                        path = "ftp://***.***.com:21/pics/" + fileName;
                        client.UploadData(path, buffer);

                    }
                }
                catch (WebException ex)
                {
                    string status = ((FtpWebResponse)ex.Response).StatusDescription;

                }
            }

            Context.Registrations.Add(new Registration
            {
                FirstName = viewModel.FirstName,
                LastName = viewModel.LastName,
                Email = viewModel.Email,
                PhoneNumber = viewModel.PhoneNumber,
                Age = viewModel.Age,
                ImagePath = path,
                CreatedDate = DateTime.Now
            });

            await Context.SaveChangesAsync();

            ConfirmationViewModel confirmViewModel = new ConfirmationViewModel
            {
                FirstName = viewModel.FirstName,
                LastName = viewModel.LastName,
                Email = viewModel.Email,
                PhoneNumber = viewModel.PhoneNumber
            };

            return RedirectToAction("Confirm", "Register", confirmViewModel);
        }
}

我希望图像应该像在本地一样保存到路径,但是在服务器上我无法通过此超时/空异常。堆栈跟踪中的异常是在寄存器控制器行89中命中方法UploadData时(在寄存器控制器中显示Submit函数)。由于此问题发生在服务器上,因此获得有关错误的反馈一直相当有限。删除try / catch我收到内部服务器错误,而使用try catch我得到Null引用异常。

我尝试的一件事是假设这里的内容为空,但删除行,但结果相同:

file.InputStream.Read(buffer, 0, buffer.Length);
file.InputStream.Close();

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

我已跟踪未设置为来自catch语句的对象异常实例的对象引用。异常似乎不是WebException,或者至少我发出的响应不是FtpWebResponse。 (由于每次都必须部署,因此很难调试),但是由于某些未知原因,我仍在超时。

答案 1 :(得分:0)

我已成功将映像从服务器保存到服务器!问题归结于专门从Web服务器域到ftp域的请求。

隐藏在NetworkSolution选项中的是文件夹特权(对任何人来说都是安全/密码保护),在允许对所需目录进行写访问之后,我切换了方法,使用HttpPostedFileBase将文件直接保存到服务器,成功!我希望我能够从一开始就找到该选项并节省数小时的头痛。