Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NETAJAX文件上载_C#_Asp.net Ajax - Fatal编程技术网

C# ASP.NETAJAX文件上载

C# ASP.NETAJAX文件上载,c#,asp.net-ajax,C#,Asp.net Ajax,我正在尝试上载图像,上载后我想在图像控件中显示它。我的代码是: <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="serv

我正在尝试上载图像,上载后我想在图像控件中显示它。我的代码是:

<form id="form1" runat="server">
    <div>

        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>

        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:FileUpload ID="FileUploadTest" runat="server" />
                <asp:Button ID="ShowImage" runat="server" Text="Show" 
                    onclick="ShowImage_Click" />
                <asp:Image ID="ImageUploaded" runat="server" Height="150px" Width="150px" 
                    ImageUrl="~/images/blankImage.gif" />
                <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
            </ContentTemplate>
            <Triggers>
                <asp:PostBackTrigger ControlID="ShowImage" />
            </Triggers>
        </asp:UpdatePanel>
    </div>
    </form>

C代码为:

protectedvoid ShowImage\u单击(对象发送方,事件参数e)
{
标签1.Text=“”;
if(FileUploadTest.HasFile)
{
尝试
{
if(FileUploadTest.PostedFile.ContentType==“image/jpeg”)
{
if(FileUploadTest.PostedFile.ContentLength<102400)
{
字符串filename=Path.GetFileName(FileUploadTest.filename);
字符串imageSavePath=Server.MapPath(“~/images/”)+文件名;
FileUploadTest.SaveAs(imageSavePath);
ImageUpload.ImageUrl=imageSavePath;
ImageUpload.Visible=true;
Label1.Text=“上传状态:文件上传!”;
}
其他的
Label1.Text=“上传状态:文件必须小于100KB!”;
}
其他的
Label1.Text=“上载状态:仅接受JPEG文件!”;
}
捕获(例外情况除外)
{
Label1.Text=“上载状态:无法上载文件。出现以下错误:“+ex.Message;
}
}
其他的
{
Label1.Text=“无文件!!!”;
}
}

但按下“显示”按钮后,图像上载成功。但是图像控制消失了。有人能帮我吗?

我想你必须将图像url设置为有效的url。检查Server.MapPath方法的结果。 您必须为图像设置URI位置


Morzel

Ajax文件上传是不可能的,但是你可以用iframe来伪装它。如果直接转到浏览器中的图像src,是否可以看到该图像?该图像从未成功上载。您不能在ajax请求中上载文件。我已经尝试了您的代码。在我的例子中,url将设置为“D:\---Source---\\MapPath\\MapPath\\images\\p51160025.JPG”。这不是一个有效的url。让我们尝试为此更改您的图像url:ImageUpload.ImageUrl=“~/images/”+文件名;
protected void ShowImage_Click(object sender, EventArgs e)
        {
            Label1.Text = "";
            if (FileUploadTest.HasFile)
            {
                try
                {
                    if (FileUploadTest.PostedFile.ContentType == "image/jpeg")
                    {
                        if (FileUploadTest.PostedFile.ContentLength < 102400)
                        {
                            string filename = Path.GetFileName(FileUploadTest.FileName);
                            string imageSavePath = Server.MapPath("~/images/") + filename;
                            FileUploadTest.SaveAs(imageSavePath);
                            ImageUploaded.ImageUrl = imageSavePath;
                            ImageUploaded.Visible = true;
                            Label1.Text = "Upload status: File uploaded!";
                        }
                        else
                            Label1.Text = "Upload status: The file has to be less than 100 kb!";
                    }
                    else
                        Label1.Text = "Upload status: Only JPEG files are accepted!";
                }
                catch (Exception ex)
                {
                    Label1.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
                }
            }
            else
            {
                Label1.Text = "No File !!!";
            }

        }