上传后临时存储的图像在哪里?

时间:2013-03-01 17:16:42

标签: asp.net

我不是一个程序员,所以我为我在这个问题上缺乏理解而道歉。我一直在挖掘所有的代码,但我无法弄清楚这个。 该网站是一个asp.net网站。编译时没有错误,除了高度,这不适用。我正在使用共享主机,因此我无法访问系统文件。

客户将图像上传到我们的网站,以便我们处理和打印他们订购的任何产品。在按下“继续付款”按钮之前,图像不会出现在服务器上的任何位置。我之前和之后都下载了整个网站以验证文件夹大小。

在将图像实际存储为.jpg之前临时存储的图像在哪里?

    </asp:UpdatePanel>
    <asp:Button CssClass="Buttons" ID="Button1" 
        style="font-weight: bold; color: white; background-color: #336699;" 
        runat="server" OnClick="AddToCartButton_Click" 
        Text="Upload Image and Add To Cart" 
        CommandArgument="ObjectDataSourceEventArgs" />
&nbsp; 
<div style="width:800px">
    <br />
    Larger files may take a minute or two to upload
    <asp:Label ID="lblLimitReached" runat="server" 
        Visible="false" STYLE="color:Red" Text="">
    </asp:Label>
</div>
<br /><hr /><br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BorderStyle="Solid"
    Caption="Shopping Cart" CellPadding="4" DataSourceID="ShoppingCartDataSource"
    ForeColor="#333333" GridLines="Vertical" CaptionAlign="Top" OnRowDeleting="GridView1_RowDeleting">
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <Columns>
        <asp:BoundField DataField="OriginalImageName" HeaderText="Image" SortExpression="OriginalImageName" />
        <asp:BoundField DataField="SizePrice" HeaderText="Size / Price" SortExpression="SizePrice" />
        <asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity" />
        <asp:BoundField DataField="MountedString" HeaderText="Mounted" SortExpression="Mounted" />
        <asp:BoundField DataField="DigitalString" HeaderText="Digital Frame" SortExpression="DigitalFrame" />
        <asp:BoundField DataField="BWString" HeaderText="Black &amp; White" SortExpression="BlackAndWhite" />
        <asp:BoundField DataField="PriceString" HeaderText="Price" ReadOnly="True" SortExpression="PriceString" />
        <asp:CommandField ButtonType="Button" ShowDeleteButton="True" />
    </Columns>
    <RowStyle BackColor="#EFF3FB" />
    <EditRowStyle BackColor="#2461BF" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:ObjectDataSource ID="ShoppingCartDataSource" runat="server" DataObjectTypeName="ImageInfo"
    DeleteMethod="DeleteData" InsertMethod="InsertData" SelectMethod="SelectData"
    TypeName="ImageInfoCollection" UpdateMethod="UpdateData" OnObjectCreating="ShoppingCartDataSource_ObjectCreating" ></asp:ObjectDataSource>
<hr /><br />
<asp:Label ID="TotalPriceLabel" runat="server" style="display:inline" Text="Current total: $0.00" Width="162px"></asp:Label>
    <br />
<asp:Button CssClass="Buttons" ID="CheckOutButton" runat="server" 
    Text="Go To Check Out" CausesValidation="False" 
    OnClick="PlaceOrderButton_Click" 
    style="font-weight: bold; color: white; background-color: #336699;" 
    Visible="False" /><br />
<asp:Label ID="FreeShipLabel" runat="server" ForeColor="#FF0033"></asp:Label><br />
<asp:XmlDataSource ID="ImageSizeDataSource" runat="server" DataFile="~/App_Data/ImageSizeData.xml"></asp:XmlDataSource>
<asp:RequiredFieldValidator ID="ImagePathValidator" runat="server" ControlToValidate="FileUpload1"
    ErrorMessage="Please select an image to upload." Display="None"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="FileUploadTypeValidator" runat="server" ControlToValidate="FileUpload1"
    Display="None" ErrorMessage="Please upload only files of type jpg (or jpeg)."
    ValidationExpression="(.*\.jpg)|(.*\.jpeg)|(.*\.JPG)|(.*\.JPEG)"></asp:RegularExpressionValidator>
<asp:RequiredFieldValidator ID="ImageSizeValidator" runat="server" ControlToValidate="SizeDropDownList"
    ErrorMessage="Please select an image size." Display="None"></asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="QuantityValidator" runat="server" ControlToValidate="QuantityTextBox"
    ErrorMessage="Please select image quantity." Display="None"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="QuantityExpressionValidator" runat="server" ControlToValidate="QuantityTextBox"
    Display="None" ErrorMessage="Invalid quantity: please choose 1 to 999." ValidationExpression="^([1-9]|[1-9][0-9]|[1-9][0-9][0-9])$"></asp:RegularExpressionValidator>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="List"
    HeaderText="Something is missing:" Height="40px" ShowMessageBox="True"
    ShowSummary="False" Width="179px" />
<br />
<div style="width:800px">
    <uc3:Footer ID="Footer1" runat="server" />
</div>
</form>

这是buynow.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Xml; 
using System.Drawing;

public partial class BuyNow : System.Web.UI.Page
{
const string DFText = "Choose a Digital Frame.";

/// <summary>
/// Page_Load
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
    Page PP = Page.PreviousPage as ASP.home_aspx;

    if (!IsPostBack)
    {
        ImageInfoCollection ImageInfo = new ImageInfoCollection();
        Session["ImageInfoCollection"] = ImageInfo;
        ResetDefaults();
        FreeShipLabel.Text = "";
        FileUpload1.Focus();
    }
}


/// <summary>
/// AddToCartButton_Click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void AddToCartButton_Click(object sender, EventArgs e)
{

    ImageInfoCollection Images = (ImageInfoCollection)Session["ImageInfoCollection"];
    if (Images == null) {
        Session["SessionError"] = "No Images on BuyNow->AddToCartButton_Click";
        Response.Redirect("Error.aspx");
    }


    if (Images.Count >= 20)
    {
        lblLimitReached.Text = "Only 20 Images allowed per order...Please place another order";
        lblLimitReached.Visible = true;
        return;
    }
    /// BUG: need to figure out how to handle the Back Button case

    if (DigitalFramesCheckBox.Checked && (DigitalFrameLabel.Text == "" || DigitalFrameLabel.Text == DFText))
        return;

    ImageInfo Image = CreateImageInfoFromPage();
    if (Image == null)
        return;

    Images.InsertData(Image);
    Session["ImageInfoCollection"] = Images;
    ShoppingCartDataSource.Select();

    GridView1.DataBind(); // update the GridView shopping cart
    TotalPriceLabel.Text = "Current Total: " + CalculateCartPrice(Images);
    ResetDefaults();

    if (Images.FreeShipping())
        FreeShipLabel.Text = "Order Qualifies for Free Shipping.";
    else
        FreeShipLabel.Text = "";

    if (Images.Count > 0)
        CheckOutButton.Visible = true;

}


/// <summary>
/// ShoppingCartDataSource_ObjectCreating
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ShoppingCartDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
    // Code that runs when a new session is started
    ImageInfoCollection Images = (ImageInfoCollection)Session["ImageInfoCollection"];
    if (Images == null) {
        Session["SessionError"] = "No Images on ShoppingCartDataSource_ObjectCreating";
        Response.Redirect("Error.aspx");
    }

    e.ObjectInstance = Images;
}

/// <summary>
/// GridView1_RowDeleting
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    ImageInfoCollection Images = (ImageInfoCollection)Session["ImageInfoCollection"];
    if (Images == null)
        return;

    Images.DeleteData(e.RowIndex);
    Session["ImageInfoCollection"] = Images;

    TotalPriceLabel.Text = "Current Total: " + CalculateCartPrice(Images);

    if (Images.FreeShipping())
        FreeShipLabel.Text = "Order Qualifies for Free Shipping.";
    else
        FreeShipLabel.Text = "";

    if (Images.Count == 0)
        CheckOutButton.Visible = false;
}

/// <summary>
/// SizeDropDownList_SelectedIndexChanged
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void SizeDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
    // read the xml file to determine image size attributes
    int index = SizeDropDownList.SelectedIndex;
    ImageXmlAttributes Attrs = new ImageXmlAttributes(ImageSizeDataSource);
    Attrs.LoadByIndex(index);

    // translate the attributes into controls for the page
    // QuantityListBox.Text = Convert.ToString(Attrs.Quantity);
    if (Attrs.MountingAvailable) {
        MountingCheckBox.Enabled = true;
        MountingPriceLabel.Enabled = true;
        MountingPriceLabel.Text = "$" + Convert.ToString(Attrs.MountingPrice);
    }
    else  {
        MountingCheckBox.Enabled = false;
        MountingCheckBox.Checked = false;
        MountingPriceLabel.Enabled = false;
        MountingPriceLabel.Text = "";
    }

    if (Attrs.DigitalAvailable) {
        DigitalFramesCheckBox.Enabled = true;
        DigitalFrameLabel.Text = "";
    }
    else {
        DigitalFramesCheckBox.Enabled = false;
        DigitalFramesCheckBox.Checked = false;
        DigitalFramePanel.Visible = false;
        DigitalFrameLabel.Text = "";
    }

    if (Attrs.BWAvailable)
        BlackAndWhiteCheckBox.Enabled = true;
    else {
        BlackAndWhiteCheckBox.Enabled = false;
        BlackAndWhiteCheckBox.Checked = false;
    }

    CardPanel.Visible = false;

    if ((SizeDropDownList.Text == "DLCCU1") || (SizeDropDownList.Text == "DLCCU2") || (SizeDropDownList.Text == "DLCCU3"))
    {
        CardPanel.Visible = true;
     }
}

protected void MountingCheckBox_CheckedChanged(object sender, EventArgs e)
{
    if (!MountingCheckBox.Checked)
        MountingPriceLabel.Text = "";
    else {
        // read the xml file to determine image size attributes
        int index = SizeDropDownList.SelectedIndex;
        ImageXmlAttributes Attrs = new ImageXmlAttributes(ImageSizeDataSource);
        Attrs.LoadByIndex(index);
        MountingPriceLabel.Text = "$" + Convert.ToString(Attrs.MountingPrice);
    }
}

/// <summary>
/// PlaceOrderButton_Click
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void PlaceOrderButton_Click(object sender, EventArgs e)
{
    Response.Redirect("~/CustomerInfo.aspx");
}

/// <summary>
/// CreateImageFromPage
/// </summary>
/// <returns></returns>
private ImageInfo CreateImageInfoFromPage()
{
    ImageInfo Image = new ImageInfo( ImageSizeDataSource );
    Image.MyFileUpload = FileUpload1;
    Image.OriginalImageName = FileUpload1.FileName; 
    Image.Mounted = MountingCheckBox.Checked;
    Image.BlackAndWhite = BlackAndWhiteCheckBox.Checked;

    Image.DigitalFrame = DigitalFramesCheckBox.Checked;
    Image.DigitalFrameID = DigitalFrameLabel.Text;

    Image.Quantity = Convert.ToInt32(QuantityTextBox.Text);
    Image.LoadAttributes(SizeDropDownList.SelectedIndex);
    return Image;
}

/// <summary>
/// ResetDefaults
/// </summary>
private void ResetDefaults()
{
    SizeDropDownList.ClearSelection();
    QuantityTextBox.Text = "1";
    MountingCheckBox.Checked = false;
    MountingPriceLabel.Text = "";
    MountingCheckBox.Enabled = true;
    BlackAndWhiteCheckBox.Checked = false;
    BlackAndWhiteCheckBox.Enabled = true;
    DigitalFramesCheckBox.Checked = false;
    DigitalFramesCheckBox.Enabled = true;
    DigitalFramePanel.Visible = false;
    CardPanel.Visible = false;
    CardPanel.Visible = false;
    DigitalFrameLabel.Text = "";
    DigitalFrameLabel.ForeColor = Color.Black;
}

/// <summary>
/// CalculateCartPrice
/// </summary>
/// <param name="Images"></param>
/// <returns></returns>
private static string CalculateCartPrice(ImageInfoCollection Images)
{
    return Images.GetTotalPriceString();
}

protected void DigitalFramesCheckBox_CheckedChanged(object sender, EventArgs e)
{
    TurnOffAllRadioButtons();
    if (DigitalFramesCheckBox.Checked == true) {
        DigitalFramePanel.Visible = true;
        DigitalFrameLabel.Text = DFText;
        DigitalFrameLabel.ForeColor = Color.Red;
        SizeDropDownList.SelectedValue = "DL20U";
        BlackAndWhiteCheckBox.Checked = false;
        BlackAndWhiteCheckBox.Enabled = false;
    }
    else {
        DigitalFramePanel.Visible = false;
        DigitalFrameLabel.ForeColor = Color.Black;
        DigitalFrameLabel.Text = "";
        SizeDropDownList.SelectedValue = "";
        BlackAndWhiteCheckBox.Checked = false;
        BlackAndWhiteCheckBox.Enabled = true;
    }
}

protected void RadioButtonF1_CheckedChanged(object sender, EventArgs e)
{
    SelectRadioButton(RadioButtonF1, e);
}

protected void RadioButtonF2_CheckedChanged(object sender, EventArgs e)
{
    SelectRadioButton(RadioButtonF2, e);
}

protected void RadioButtonF3_CheckedChanged(object sender, EventArgs e)
{
    SelectRadioButton(RadioButtonF3, e);
}

protected void RadioButtonF4_CheckedChanged(object sender, EventArgs e)
{
    SelectRadioButton(RadioButtonF4, e);
}

protected void RadioButtonF5_CheckedChanged(object sender, EventArgs e)
{
    SelectRadioButton(RadioButtonF5, e);
}

protected void RadioButtonF6_CheckedChanged(object sender, EventArgs e)
{
    SelectRadioButton(RadioButtonF6, e);
}

protected void RadioButtonF7_CheckedChanged(object sender, EventArgs e)
{
    SelectRadioButton(RadioButtonF7, e);
}

protected void RadioButtonF8_CheckedChanged(object sender, EventArgs e)
{
    SelectRadioButton(RadioButtonF8, e);
}

protected void RadioButtonF9_CheckedChanged(object sender, EventArgs e)
{
    SelectRadioButton(RadioButtonF9, e);
}

protected void RadioButtonF13_CheckedChanged(object sender, EventArgs e)
{
    SelectRadioButton(RadioButtonF13, e);
}

protected void RadioButton15_CheckedChanged(object sender, EventArgs e)
{
    SelectRadioButton(RadioButtonF15, e);
}

protected void RadioButtonF16_CheckedChanged(object sender, EventArgs e)
{
    SelectRadioButton(RadioButtonF16, e);
}

protected void RadioButtonF17_CheckedChanged(object sender, EventArgs e)
{
    SelectRadioButton(RadioButtonF17, e);
}

protected void RadioButtonF18_CheckedChanged(object sender, EventArgs e)
{
    SelectRadioButton(RadioButtonF18, e);
}

private void SelectRadioButton(RadioButton RB, EventArgs e)
{
    TurnOffAllRadioButtons();
    RB.Checked = true;
    DigitalFrameLabel.Text = RB.ToolTip;
    DigitalFrameLabel.ForeColor = Color.Black;
}

private void TurnOffAllRadioButtons()
{
    RadioButtonF1.Checked = false;
    RadioButtonF2.Checked = false;
    RadioButtonF3.Checked = false;
    RadioButtonF4.Checked = false;
    RadioButtonF5.Checked = false;
    RadioButtonF6.Checked = false;
    RadioButtonF7.Checked = false;
    RadioButtonF8.Checked = false;
    RadioButtonF9.Checked = false;
    RadioButtonF1.Checked = false;
    RadioButtonF13.Checked = false;
    RadioButtonF15.Checked = false;
    RadioButtonF16.Checked = false;
    RadioButtonF17.Checked = false;
    RadioButtonF18.Checked = false;
}

protected void BlackAndWhiteCheckBox_CheckedChanged(object sender, EventArgs e)
{
    if (BlackAndWhiteCheckBox.Checked) {
        DigitalFramesCheckBox.Checked = false;
        DigitalFramesCheckBox.Enabled = false;
    }
    else {
        if ((SizeDropDownList.SelectedValue == "DL20U") || (SizeDropDownList.SelectedValue == ""))
        {
            DigitalFramesCheckBox.Enabled = true;
            DigitalFramesCheckBox.Checked = false;
        }
    }
}

}

2 个答案:

答案 0 :(得分:1)

以下几行是问题所在:

    Images.InsertData(Image);
    Session["ImageInfoCollection"] = Images;
    ShoppingCartDataSource.Select();

基本上,当图像上传时,它们存储在会话对象中。这意味着它将直接进入Web服务器RAM并停留在那里。这是一个非常糟糕的主意,特别是对于期望任何类型的负载的服务器。顺便说一句,其原因在于它导致RAM使用量大幅增加,如果Web应用需要回收(在这种情况下很可能经常发生),那么会话中存储的所有图像都将丢失。

需要将其更改为将图像推送到数据库或磁盘上的临时文件。无论哪种方式,如何对其进行编码意味着在将文件从会话移动到磁盘之前,您不会看到任何文件出现在文件系统上。

答案 1 :(得分:-1)

当您使用像文件上传这样的控件时,上传的文件会放在Web应用程序服务器上的某个临时文件夹中。它放置的位置确实无关紧要,因为asp.net为您提供了一个可供使用的对象。例如:

<asp:FileUpload ID="FileUpload" runat="server" Height="24px" Style="position: static" Width="255px"  />
<asp:Button ID="Uploadbtn" runat="server" OnClick="Uploadbtn_Click" Style="position: static" Text="Upload" Width="82px" />

此控件创建典型的上传控件。它有一个浏览按钮和一个上传按钮。当用户单击上传按钮时,您将拥有如下事件方法:

Protected Sub Uploadbtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
   Dim myUploadeObject As HttpPostedFile
   myUploadeObject = Me.FileUpload.PostedFile       
End Sub

HttpPostedFile对象有一大堆可以访问的方法和属性,比如文件名,输入流,内容类型和长度。

此外,fileUPload对象本身有许多有用的属性和方法,如:HasFile,文件名,文件字节和内容

将他们聚集在一起,你可以做这样的事情:

 Protected Sub Uploadbtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Dim fu As FileUpload
    fu = Me.FileUpload

    If (fu.HasFile = True) Then
        Dim myUploadeObject As HttpPostedFile
        myUploadeObject = Me.FileUpload.PostedFile

        Dim filePath As String = myUploadeObject.FileName
        Dim fileName As String = fu.FileName
        Dim fileBytes As Byte() = fu.FileBytes
        Dim fileContenetType As String = myUploadeObject.ContentType
        Dim inputStream As IO.FileStream = myUploadeObject.InputStream

    Else
        'there was a problem, alert the user
    End If      
End Sub