[转发]图像文件名未存储 [英] [Repost] image file name not stored

查看:67
本文介绍了[转发]图像文件名未存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经上传了照片,我已经选择了本地主机(示例D:kannan \ photo),如何在我的代码中进行测试

i have upload the photo i have select the local host (Example D:kannan\photo ) how toc hane in my code

namespace photoshops
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            onflbload(sender, e);
        }
           public void onflbload(object sender, EventArgs e)
        {
           
            
            if (flbload.HasFile)
            {
<pre lang="cs">HttpPostedFile file = UploadedFile.PostedFile;
            string fileExt = Path.GetExtension(file.FileName).ToLower();
            string fileName = Path.GetFileName(file.FileName);
           
                if (fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".bmp" || fileExt == ".jpeg" || fileExt == ".png")
                    {
                         file.SaveAs("D://" + fileName);
                    }
               }
              
            
            SqlConnection connection = new SqlConnection();
            connection.ConnectionString =@"Data Source=DEVI\SQLEXPRESS; Initial Catalog =cat; Integrated Security=SSPI";
            try
            {
                connection.Open();
                SqlCommand cmd = new SqlCommand("insert into tblphotosettings " + 
                                                "(BillNo,CustomerName,Address,StartDate,EndDate,Systemurl,Numberofcopies,Amount,Total ) values (@BillNo,@CustomerName,@Address,@StartDate,@EndDate,@Systemurl,@Numberofcopies,@Amount,@Total)", connection);
                cmd.Parameters.Add("@BillNo", SqlDbType.NVarChar).Value = TextBox1.Text;
                cmd.Parameters.Add("@CustomerName", SqlDbType.NVarChar).Value = TextBox2.Text;
                cmd.Parameters.Add("@Address", SqlDbType.NVarChar).Value = TextBox3.Text;
                cmd.Parameters.Add("@StartDate", SqlDbType.NVarChar).Value = Rdbsdate.SelectedDate;
                cmd.Parameters.Add("@EndDate", SqlDbType.NVarChar).Value = Rdbddate.SelectedDate;
                cmd.Parameters.Add("@Systemurl", SqlDbType.VarChar).Value = flbload ;
                cmd.Parameters.Add("@Numberofcopies", SqlDbType.NVarChar).Value = TextBox7.Text;
                cmd.Parameters.Add("@Amount", SqlDbType.NVarChar).Value = TextBox8.Text;
                cmd.Parameters.Add("@Total", SqlDbType.NVarChar).Value = TextBox9.Text;
                cmd.ExecuteNonQuery();
            }
            
            finally
            {
                connection.Close();
            }
        }
    }
}





该页面已运行,但是我选择了一张图片,然后单击保存"按钮
错误文件路径无效

[edit]添加了代码块以保留格式-OriginalGriff [/edit]





the page is run but i have select a picture and click the save button
Error file path is not valid

[edit]Code block added to preserve formatting - OriginalGriff[/edit]

推荐答案

我认为您的代码不太像这样:
I assume that your code is not quite like this:
string filename = Server.MapPath("~/photo") +flbload.PostedFile.FileName;

As这不应生成无法识别的转义序列"错误.如果您使用的是"\"而不是"/"字符...

尝试在字符串前面加上"@",这会关闭转义字符处理,并可能使其更适合您:

As This should not generate a "unrecognised escape sequence" error. It would if you had a "\" instead of the "/" character though...

Try putting an "@" in front of the string - that turns off escape character processing, and may make it work better for you:

string filename = Server.MapPath(@"~/photo") + flbload.PostedFile.FileName;



页面运行,但是我已经使用本地主机
例如字符串文件名= Server.MapPath(@〜/D:\ photo")+ flbload.PostedFile.FileName;
但错误路径在有效范围内.我已使用文件上传控件"


不,那行不通.
Server.MapPath的整体思想是将基于内部URL的路径转换为基于PC的文件结构路径. 〜"位是网站根目录的基于URL的位.
所以您要的是路径:
"C:\ websites \ users \ UID123456 \ root/D:\ photo"严重无法使用,因为它将抱怨路径中间的冒号...
另外,您需要使用进一步的斜杠或反斜杠将路径与文件名分开.

我的操作方式:


"hi
the page run but i have use local host
Ex string filename = Server.MapPath(@"~/D:\photo") + flbload.PostedFile.FileName;
but error path is in valid .i have use file upload control"


No, that won''t work.
The whole idea of Server.MapPath is to convert an internal URL based path to a PC based file structure path. The ''~'' bit is the URL based bit for the root directory of your website.
So what you are asking for is the path:
"C:\websites\users\UID123456\root/D:\photo" which is seriously not going to work as it will complain about the colon in the middle of the path...
In addition, you need to separate the path from the filename with a further slash or backslash.

The way I would do it:

if (flbload.HasFile)
   {
   string filename = Server.MapPath(@"~/photo") + "/" + flbload.FileName;
   flbload.SaveAs(filename);
   }




您能在我的代码中发布编辑内容吗,我完全感到困惑,我是初学者"


我可以,但是其他答案的上下文丢失了.相反,这是您的代码被我的修改了:




"can you post edit in my code i have totally confused i am begineer"


I could, but then the context to other answers is lost. Instead, here is your code modified by mine:

public void onflbload(object sender, EventArgs e)
    {
    if (flbload.HasFile)
        {
        string filename = Server.MapPath(@"~/Resources/Images") + "/" + flbload.FileName;
        flbload.SaveAs(filename);
        using (SqlConnection connection = new SqlConnection())
            {
            connection.ConnectionString = @"Data Source=DEVI\SQLEXPRESS; Initial Catalog =cat; Integrated Security=SSPI";
            try
                {
                connection.Open();
                using (SqlCommand cmd = new SqlCommand("insert into tblphotosettings " +
                                                "(BillNo,CustomerName,Address,StartDate,EndDate,Systemurl,Numberofcopies,Amount,Total) " +
                                                "values (@BillNo,@CustomerName,@Address,@StartDate,@EndDate,@Systemurl,@Numberofcopies,@Amount,@Total)",
                                                connection))
                    {
                    cmd.Parameters.AddWithValue("@BillNo", TextBox1.Text);
                    cmd.Parameters.AddWithValue("@CustomerName", TextBox2.Text);
                    cmd.Parameters.AddWithValue("@Address", TextBox3.Text);
                    cmd.Parameters.AddWithValue("@StartDate", Rdbsdate.SelectedDate);
                    cmd.Parameters.AddWithValue("@EndDate", Rdbddate.SelectedDate);
                    cmd.Parameters.AddWithValue("@Systemurl", filename);
                    cmd.Parameters.AddWithValue("@Numberofcopies", TextBox7.Text);
                    cmd.Parameters.AddWithValue("@Amount", TextBox8.Text);
                    cmd.Parameters.AddWithValue("@Total", TextBox9.Text);
                    cmd.ExecuteNonQuery();
                    }
                }
            catch (Exception ex)
                {
                LogError(ex.Message);
                }
            finally
                {
                connection.Close();
                }
            }
        }
    }

您将看到我进行了许多更改!
1)有我上面讨论的mod.
2)我已经使用using块来确保SqlConnection和SqlCommand被正确关闭和处理.
3)我已将您的Parameters.Add更改为Paramaters.AddWithValue:前者已贬值,应替换为后者-更易于阅读!
4)我包含了一个catch块:这样,即使用户没有发现错误,我也会发现发生了错误!请尽量不要遗漏渔获物,因为它们可以帮助您及早发现问题,而不是隐瞒它,直到以后成为灾难!

另外两件事:
1)我的LogEntry类在您的代码中不存在:将调用替换为适当的操作:将其添加到文本日志文件或其他内容中,以便稍后查看它并找出发生了什么错误!
2)请,请,请更改控件的名称! TextBox8可能包含您的数量",但如果您编写
,它会更明显

You will see I have made a number of changes!
1) There is the mod I discussed above.
2) I have used using blocks to ensure that the SqlConnection and SqlCommand are properly closed and disposed.
3) I have changed your Parameters.Add to Paramaters.AddWithValue: the former is depreciated and should be replaced with the latter - it is easier to read!
4) I have included a catch block: this way I will find out that an error occurred, even if the user doesn''t! Please, try very hard not to omit catches, as they can help you find a problem early, rather than hidding it until it is a catastrophe later!

Two other things:
1) My LogEntry class does not exist in your code: replace the call with your appropriate action: add it to a text log file, or something so you can review it later and work out what error has occurred!
2) Please, please, please, change the names of your controls! TextBox8 may hold your "amount" but it is a lot more obvious if you write

cmd.Parameters.Add("@Amount", SqlDbType.NVarChar).Value = tbAmount.Text;

而不是

cmd.Parameters.Add("@Amount", SqlDbType.NVarChar).Value = TextBox8.Text;

它使您可以更轻松地看到您在正确的位置使用了正确的文本框-无需回头查看设计即可发现哪个文本框可以容纳份数... :laugh:

抱歉,我不知道文本日志文件"

试试这个:

It just makes it easier to see that you are using the correct text box in the correct place - and you don''t have to look back in your design to find out which text box holds the number of copies... :laugh:

"sorry i dont know text log file"

Try this:

using (StreamWriter sw = File.AppendText(Server.MapPath(@"~") + @"/MyLog.Txt"))
    {
    sw.WriteLine(ex.Message);
    }

这不是理想的方法(将其放置在通常可用的位置),但是会......
(我将数据库用于日志,因此设置起来比您现在想要的要复杂一些)

It''s not ideal (it puts it in a generally available location) but it''ll do...
(I use a database for my logs, which is a bit more complex to set up than you want at the moment)


嘿,请尝试一下

file.SaveAs("D://kannan/photo/" + fileName);
hey vimal try this

file.SaveAs("D://kannan/photo/" + fileName);


vimal22 2写道:
是1错误错误1无法识别的转义序列字符串文件名= Server.MapPath(〜/photo")+ flbload.PostedFile.FileName;
我如何存储图像路径示例(D:\ kannan \ photo),您可以发布代码


从代码中,我将假定它是一个Web应用程序(您尚未提及或标记).

您仍然不了解路径在Web应用程序中的工作方式.
Server.MapPath [
vimal22 2 wrote:
yes 1 error Error 1 Unrecognized escape sequence string filename = Server.MapPath("~/photo") +flbload.PostedFile.FileName;
how do i store image path example (D:\kannan\photo) can u post the code


From the code I will assume that it''s a web application (which you haven''t mentioned or tagged).

You still don''t understand how paths works in a web application.
Server.MapPath[^] method maps the specified relative or virtual path to the corresponding physical directory on the server. That means the path that you specified as a parameter will be mapped to the current executing file (depend on relative or virtual path that you have supplied).

Now the very first thing - If you want all the files under the directory ''photo'', you don''t need "+flbload.PostedFile.FileName;" path. So your file path will be
string filepath = Server.MapPath("~/photo");



现在第二件事,您说您要将文件存储在位置D:\ kannan \ photo.
您必须了解它在网络体系结构中无法正常工作.您将所有内容都存储在网站的根目录中.因此,您的网站根目录中将存在一个名为"photo"的文件夹,所有照片都将保存在该文件夹中.还有权限问题.因此,最好将内容存储在网站根目录中.

我还建议您阅读以下内容-
关于Asp.net初学者的路径"有一些内容 [ ^ ]

希望能给您一些想法.



Now second thing, you said you want to store the file in location D:\kannan\photo.
You must understand that it doesn''t work like this in a web architecture.You will store everything inside your web site''s root directory. So there will be a folder inside your web site root directory called ''photo'' and every photo will be saved there. There are permission issues as well. So it''s always better to store things inside the web site root directory.

I also suggest you to read this - There is something about "Paths" for Asp.net beginners[^]

Hope that gives you some idea.


这篇关于[转发]图像文件名未存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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