为什么我的fileupload控件无法在实时服务器上运行,但在我的机器上运行良好?

时间:2013-12-03 17:23:08

标签: asp.net

即时尝试上传大约500KB或更少的图片。正如我所说它在我的机器上完美运行,但在实时服务器上表现得非常奇怪。我的配置有问题,但我无法弄清楚。我继续收到此错误“无法上传拇指图像”。请帮助!

我的网络配置

    <system.web>
    <httpRuntime executionTimeout="150" maxRequestLength="307200" />
  </system.web>
  <location path="~/PropertyImages">
    <system.web>
      <httpRuntime executionTimeout="110" maxRequestLength="307200" />
    </system.web>
  </location>
  <location path="~/PropertyImages/thumbs">
    <system.web>
      <httpRuntime executionTimeout="90" maxRequestLength="112640" />
    </system.web>
  </location>
  <system.webServer>
    <security>
      <requestFiltering>
        <requestLimits maxAllowedContentLength="307200"/>
      </requestFiltering>
    </security>
  </system.webServer>

我的控制

<asp:Label ID="Image1Label" runat="server" CssClass="inputLabels" />&nbsp;<asp:FileUpload ID="image1FileUpload" runat="server" CssClass="adminUpload" />
                                <asp:Button ID="upload1Button" runat="server" Text="Upload" OnClick="upload1Button_Click" CssClass="adminButton" />

我的代码隐藏在文件

之后
protected void upload1Button_Click(object sender, EventArgs e)
    {
        Boolean fileOK = false;
        String path = Server.MapPath("~/PropertyImages/thumbs/");
        if (image1FileUpload.HasFile)
        {
            String fileExtension = System.IO.Path.GetExtension(image1FileUpload.FileName).ToLower();
            String[] allowedExtensions = { ".jpeg", ".jpg" };
            for (int i = 0; i < allowedExtensions.Length; i++)
            {
                if (fileExtension == allowedExtensions[i])
                {
                    fileOK = true;
                }
            }
        }

        if (fileOK)
        {
            try
            {
                int fileSize = image1FileUpload.PostedFile.ContentLength;
                if (fileSize > 112640)
                {
                    statusLabel.Text = "thumb image size exceeded.";
                }
                else
                {                
                    image1FileUpload.PostedFile.SaveAs(path + image1FileUpload.FileName);
                    statusLabel.Text = "thumb image uploaded!";
                    // update database with new property details
                    PropertyDetails pd = CatalogAccess.GetPropertyDetails(currentPropertyId);
                    CatalogAccess.UpdatePropertyThumbnailFrontImage(currentPropertyId, image1FileUpload.FileName);
                    // reload the page 
                    Response.Redirect("AdminPropertyDetails.aspx" +
                            "?DepartmentID=" + currentDepartmentId +
                            "&CategoryID=" + currentCategoryId +
                            "&PropertyID=" + currentPropertyId);

                }


            }
            catch (Exception ex)
            {
                statusLabel.Text = "thumb image cannot be uploaded.";
            }
        }
        else
        {
            statusLabel.Text = "Cannot accept files of this type.";
        }


    }

1 个答案:

答案 0 :(得分:0)

基于您将image1FileUpload.FileName传递给UpdatePropertyThumbnailFrontImage方法的事实,我假设此方法正在尝试从该路径读取文件?

那不行。 FileName是客户端上文件的路径 - 有时只是名称。服务器上的代码无法读取客户端上的文件。它只适用于您在本地测试它,因为客户端和服务器是相同的。

您需要使用PostedFile property来检索HttpPostedFile object,并使用其InputStream propertySaveAs method,或者您需要调用SaveAs method FileUpload control将上传的文件保存在服务器上的某个位置。

例如:

string path = Server.MapPath("~/PropertyImages/thumbs/");
string name = System.IO.Path.GetFileName(image1FileUpload.FileName);
path = System.IO.Path.Combine(path, name);
image1FileUpload.SaveAs(path);

CatalogAccess.UpdatePropertyThumbnailFrontImage(currentPropertyId, path);