在c#

时间:2016-02-22 10:47:48

标签: c# file-upload

我正在开发一个可以保存和删除本地系统上传文件的项目。这是示例代码,我在asp.net c#

中使用fileupload asp-control
List<AttachmentModel> attachmentList = new List<AttachmentModel>();

        if (fuAttachment.HasFiles)
        {
            foreach (HttpPostedFile uploadedFile in fuAttachment.PostedFiles)
            {
                attachment.AttachmentType = "Attachment";
                attachment.FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);
                                   uploadedFile.SaveAs(Path.Combine(Server.MapPath(@"~/Attachments/" + uploadedFile)));
                attachmentList.Add(attachment);
            }
        }
        objEL.AttachmentList = attachmentList;

我正在尝试上传多个文件(&gt; = 2个文件)。但我只能读取一个首先被选中的文件..

所以我改变了一行

attachment.FileName = Path.GetFileName(fuAttachment.PostedFile.FileName);

attachment.FileName = uploadedFile.FileName;

但有了这个,我得到文件的整个路径为

  

&#34; C:\ Users \用户ThirupathiReddy \下载\ PSK_logo.jpg&#34;

我只想阅读文件名......

谢谢..

1 个答案:

答案 0 :(得分:0)

尝试:

var fullFilename = @"C:\Users\ThirupathiReddy\Downloads\PSK_logo.jpg";
var fileInfo = new FileInfo(fullFilename );
var filename = fileInfo.Name;