如何使用ASP.NET从ajaxfileupload获取文件名 [英] How to get filename from ajaxfileupload using ASP.NET

查看:96
本文介绍了如何使用ASP.NET从ajaxfileupload获取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么我在尝试检索文件名时得到一个空字符串值。为了更好地理解我的意思。为了从AjaxFileUpload控件获取文件名,我将执行此代码(在类名为CreateBrands的类中):



我尝试过:



I was wondering why i'm getting an empty string value when i try to retrieve a filename. To better understand what i meant. In order to get the filename from the AjaxFileUpload control, I would do this code(which is in class named CreateBrands):

What I have tried:

string filename = Path.GetFileName(e.FileName);
ConnectionClassGuitarItems.stringInstrumentFileName1 = filename;
itemFileUpload1.SaveAs(Server.MapPath("~/Images/Brands/String Instrument Items/Guitar/") + filename);





如您所见,我将字符串文件名传递给ConnectionClassGuitarItems.stringInstrumentFileName1,这是一个名为ConnectionClassGuitarItems的类中的属性。这是ConnectionClassGuitarItems中的一些代码。我没有把所有内容都包括在内,因为它与问题无关。





As you can see, i passed the string filename to ConnectionClassGuitarItems.stringInstrumentFileName1, which is a property in a class called ConnectionClassGuitarItems. Here is a bit of code in the ConnectionClassGuitarItems. I did not include everything in there since it is not related to the question.

public static class ConnectionClassGuitarItems
{

public static string stringInstrumentFileName1 { get; set; }
}





之后,我会回到CreateBrands类,我会用它来添加它我的数据库。这是另一段代码:





After that, I would go back to the class CreateBrands and I would use that property to add it in my database. Here is another piece of the code:

 protected void Button1_Click(object sender, EventArgs e)
{

        if (itemType1.Checked)
        {
        try {

            string item_image1 = ConnectionClassGuitarItems.stringInstrumentFileName1;
            string item_image2 = ConnectionClassGuitarItems.stringInstrumentFileName2;

            ConnectionClassGuitarItems.AddStringInstrumentItems(item_image1,item_image2);





这里是我最后将它添加到数据库:





And here is where i finally add it into the database:

public static void AddStringInstrumentItems(string itemimage1, string 
 itemimage2)
{
    MusicStoreDBEntities obj = new MusicStoreDBEntities();

    instrumentItem s = new instrumentItem();
    s.itemimage1 = itemimage1;
    s.itemimage2 = itemimage2;

    obj.instrumentItems.Add(s);
    obj.SaveChanges();
}





这基本上就是代码的流程。我的问题是,文件名将始终为空。如果我做错了导致它传递一个空的文件名,请建议或给出解决方案。



That's basically the flow of the code. My problem is, the filename would always be empty. Kindly advise or give solution on this if i'm doing something wrong that is causing it to pass a filename that is empty.

推荐答案

您可以轻松获取文件名写作

You can easily get the file name by writing
string filename1 = Path.GetFileName(FileUpload1.PostedFile.FileName);


这篇关于如何使用ASP.NET从ajaxfileupload获取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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