用于在特定文件夹中上传图像的代码 [英] code to upload image in a specific folder

查看:63
本文介绍了用于在特定文件夹中上传图像的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我写了一个代码来上传特定文件夹中的图片。编码是

Hi All,

I have written a code to upload image in a specific folder. The coding is

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
If FileUpload1.HasFile Then
           Try
               Dim filename As String = Path.GetFileName(FileUpload1.FileName)
               FileUpload1.SaveAs(Server.MapPath("~/Images") & filename)
               StatusLabel.Text = "Upload status: File uploaded!"
           Catch ex As Exception
               StatusLabel.Text = "Upload status: The file could not be uploaded."
           End Try
       End If
End Sub



源代码是


And the source code is

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Upload" />

<asp:Label ID="StatusLabel" runat="server" text="Upload status: " />



但是图像没有存储。任何人都可以帮助我吗?


But the image is not stored. Can any one help me on this?

推荐答案

吃掉你捕获的异常的insterad,你可能想把它记录到一个文件或应用程序事件日志中。 />


但是,现在,从代码中删除Try / Catch块,这样你就可以看到它正在抛出的错误。



但是,在快速浏览之后,您将上传的文件名附加到字符串〜/ Images。您的附加名称将如下所示:〜/ ImagesSomeImageIUploaded.jpg。这看起来像是一条有效的道路吗?阅读Path类的文档,你会发现一个名为Combine的方法,它将为你创建一个路径字符串。
Insterad of eating the exception you caught, you might want to log it to a file or the Application Event Log.

But, for now, remove the Try/Catch block from your code so you can see the error that it's throwing.

But, after a quick glance, you're appending the filename of the upload to the string "~/Images". Your appended name will look like this: "~/ImagesSomeImageIUploaded.jpg". Does that look like a valid path to you?? Read the documentation on the Path class and you'll find a method called "Combine" that will create a path string for you.


我想下面的代码帮助你



I think below code help u

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
       Dim path As String
       path = Server.MapPath("~/Images/")
       If (FileUpload1.HasFile) Then
           path = path + FileUpload1.FileName
           FileUpload1.SaveAs(path)

'If you want that uploaded image...
           'Dim img As String
           'img = "~/Images/" + FileUpload1.FileName
           'Image1.ImageUrl = img
       End If
   End Sub


string filename = File.FileName;

string filename=File.FileName;
path = Server.MapPath("~/Images/")
<pre lang="cs">filePath = System.IO.Path.Combine(path , filename);
               // file is uploaded
               file.SaveAs(filePath);</pre>











OR

string fileWithPath=Server.MapPath("~/Images"/;)+FileName.Jpg;




file.SaveAs(fileWithPath)







您需要提供带扩展名的文件名。然后它会得到保存



希望这有助于!!!




you need to provide name of the file with extension. Then it will get save

Hope This Helps!!!


这篇关于用于在特定文件夹中上传图像的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆