| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1019 人关注过本帖
标题:图片上传问题
只看楼主 加入收藏
huoqilin
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2007-7-20
收藏
 问题点数:0 回复次数:4 
图片上传问题
protected   void   Button1_Click(object   sender,   EventArgs   e)
        {
                string   fullname   =   this.FileUpload1.FileName.ToString();
                string   fn   =   System.DateTime.Now.ToString("yyyyMMddHHmmss")   +   fullname;
                string   typ2   =   fullname.Substring(fullname.LastIndexOf(".")   +   1);
                string   size   =   this.FileUpload1.PostedFile.ContentLength.ToString();

                if   (typ2   ==   "gif"   ? ?   typ2   ==   "jpg"   ? ?   typ2   ==   "bmp"   ? ?   typ2   ==   "png")
                {
                        this.FileUpload1.SaveAs(Server.MapPath("../upimage")   +   "\\"   +   fn);
                        this.Image1.Visible   =   true;
                        this.Image1.ImageUrl   =   "../upimage/"   +   fn;
                        OleDbConnection   conn   =   new   OleDbConnection(ConfigurationManager.AppSettings["mdb"]);
                        OleDbCommand   insert   =   new   OleDbCommand("insert   into   photo   (fenlei,biaoti,url,pho,addtime)   values   ('"   +   this.DropDownList1.SelectedValue   +   "','"   +   this.TextBox1.Text.ToString()   +   "','"   +   this.TextBox2.Text.Trim()   +   "','"   +   fn   +   "','"   +   System.DateTime.Now   +   "')",   conn);
                        conn.Open();
                        insert.ExecuteReader();
                        conn.Close();
                        Label1.Text   =   "上传成功"   +   fn;
                }
                else
                {
                        Label1.Text   =   "文件类型不支持,只能上传   .jpg   .gif   .bmp   .png   格式的图片";
                }

        }

现在出现问题,iis不设置虚拟目录的时候,上传正常。
只要一设置虚拟目录,上传的时候,就弹出对话框提示   “未将对象引用设置到对象的实例”
请高手帮忙。
搜索更多相关主题的帖子: string fullname ToString sender 
2007-11-24 10:13
bygg
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:乖乖的心中
等 级:版主
威 望:241
帖 子:13555
专家分:3076
注 册:2006-10-23
收藏
得分:0 
干嘛要设置虚拟目录啊?

飘过~~
2007-11-24 13:02
FenteLi
Rank: 1
来 自:上海
等 级:新手上路
帖 子:124
专家分:0
注 册:2007-11-24
收藏
得分:0 
应该是路径问题。把出错部分代码用红线标出来,我看一下。
2007-11-24 13:39
huoqilin
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2007-7-20
收藏
得分:0 
原帖由 [bold][underline]FenteLi[/underline][/bold] 于 2007-11-24 13:39 发表 [url=http://bbs.][/url]
应该是路径问题。把出错部分代码用红线标出来,我看一下。


错误提示是弹出框,看不出来出错在第几行...
把其他的控件全删除,只保留个上传,还是一样的错误
应该就是上传有问题。

在iis里面不设置虚拟目录,上传是正常的。
也就是说,把这个网站当成一个文件夹,放在其他网站里面正常。

2007-11-26 08:59
FenteLi
Rank: 1
来 自:上海
等 级:新手上路
帖 子:124
专家分:0
注 册:2007-11-24
收藏
得分:0 
这个是MSDN上的实例,你参考一下。我测试过了,好用,但是你要上传得那个文件夹的权限一定要全开阿。不然也是不行的。在下面程序中t这个文件夹的权限要能写啊。
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.

<script runat="server">

    protected void UploadButton_Click(object sender, EventArgs e)
    {
        // Specify the path on the server to
        // save the uploaded file to.
        String savePath = @"E:\t\";

        // Before attempting to perform operations
        // on the the file, verify that the FileUpload
        // control contains a file.
        if (FileUpload1.HasFile)
        {
            // Append the name of the file to upload to the path.
            savePath += FileUpload1.FileName;
            
            // Call the SaveAs method to save the
            // uploaded file to the specified path.
            // This example does not perform all
            // the necessary error checking.               
            // If a file with the same name
            // already exists in the specified path,  
            // the uploaded file overwrites it.
            FileUpload1.SaveAs(savePath);
               
            // Notify the user that the file was uploaded successfully.
            UploadStatusLabel.Text = "Your file was uploaded successfully.";
               
            // Call a helper routine to display the contents
            // of the file to upload.
            DisplayFileContents(FileUpload1.PostedFile);
        }
        else
        {
            // Notify the user that a file was not uploaded.
            UploadStatusLabel.Text = "You did not specify a file to upload.";
        }
    }
    void DisplayFileContents(HttpPostedFile file)
    {
         myStream;
        Int32 fileLen;
        StringBuilder displayString = new StringBuilder();
     
        // Get the length of the file.
        fileLen = FileUpload1.PostedFile.ContentLength;
            
        // Display the length of the file in a label.
        LengthLabel.Text = "The length of the file is " +
                           fileLen.ToString() + " bytes.";
   
        // Create a byte array to hold the contents of the file.
        Byte[] Input = new Byte[fileLen];
            
        // Initialize the stream to read the uploaded file.
        myStream = FileUpload1.FileContent;
            
        // Read the file into the byte array.
        myStream.Read(Input, 0, fileLen);
        
        // Copy the byte array to a string.
        for (int loop1 = 0; loop1 < fileLen; loop1++)
        {
            displayString.Append(Input[loop1].ToString());
        }

        // Display the contents of the file in a
        // textbox on the page.
        ContentsLabel.Text = "The contents of the file as bytes:";

        TextBox ContentsTextBox = new TextBox();
        ContentsTextBox.TextMode = TextBoxMode.MultiLine;
        ContentsTextBox.Height = Unit.Pixel(300);
        ContentsTextBox.Width = Unit.Pixel(400);
        ContentsTextBox.Text = displayString.ToString();

        // Add the textbox to the Controls collection
        // of the Placeholder control.
        PlaceHolder1.Controls.Add(ContentsTextBox);

    }
</script>

<html xmlns="http://www. >
<head runat="server">
    <title>FileUpload.FileContent Property Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h4>Select a file to upload:</h4>
      
        <asp:FileUpload id="FileUpload1"
           runat="server">
        </asp:FileUpload>
      
        <br /><br />
      
        <asp:Button id="UploadButton"
            Text="Upload file"
            OnClick="UploadButton_Click"
            runat="server">
        </asp:Button>
        
        <br /><br />
        
        <asp:Label id="UploadStatusLabel"
           runat="server">
        </asp:Label>  
            
        <hr />
        
        <asp:Label id="LengthLabel"
           runat="server">
        </asp:Label>  
        
        <br /><br />
      
        <asp:Label id="ContentsLabel"
           runat="server">
        </asp:Label>  
        
        <br /><br />
      
        <asp:PlaceHolder id="PlaceHolder1"
            runat="server">
        </asp:PlaceHolder>        
    </div>
    </form>
</body>
</html>
2007-11-26 10:11
快速回复:图片上传问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019320 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved