带有RadUpload文本框的C#中的FilePath

时间:2016-07-21 07:51:56

标签: c# filepath

我有这段代码可以验证文件名是否过长。

protected void buttonSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (pomDoc != null)
                {
                    //load pomContext
                    if (pomContext == null)
                    {
                        InitContext();
                    }

                    if (RadUpload1.UploadedFiles.Count <= 0)
                    {
                        Session[AppConstants.ERROR_MESSAGE] = ErrorsList.GetErrorMessage(
                            ErrorsList.ERR_P_DATE_FILE_VALID);
                    }
                    else
                    {
                        {
                            foreach (UploadedFile validFile in RadUpload1.UploadedFiles)
                            {
                                pomDoc = (IDbContextualRecord) Session[AppConstants.POM_DOCUMENT_NEW];

                                FileInfo fi = new FileInfo(validFile.FileName);
                                Stream fs = validFile.InputStream;

                                IDbContextualRecord pomFile = pomContext.CreateAndAddRecordForInsert(PomFile.t_name);
                                pomFile[PomFile.c_pomDocumentId] = pomDoc[PomDocument.c_id];
                                string targetFolder = AppSession.Current.ConfigParameters[AppConstants.UPLOAD_FILE_PATH] + "\\POM\\" +
                                                      pomDoc.GetField("id").GetString();
                                if (!Directory.Exists(targetFolder))

                                {
                                    Directory.CreateDirectory(targetFolder);
                                }

                                long bytesOnTheStream = 0L;
                                try
                                {

                                    DirectoryInfo dir = new DirectoryInfo(targetFolder);
                                    if (dir.Exists == false)
                                        dir.Create();

                                    string fullFileName = Path.Combine(targetFolder, fi.Name);

                                    if (fullFileName.Length > 260)
                                    {
                                        throw new BaseApplicationException(ErrorsList.ERR_P_UPLOAD_FILE_NAME, ErrorsList.GetErrorMessage(ErrorsList.ERR_P_UPLOAD_FILE_NAME));

                                        }
                                    Stream targetStream = File.OpenWrite(fullFileName);
                                    byte[] buffer = new Byte[AppConstants.BUFF_SIZE];
                                    int bytesRead;

                                    // while the read method returns bytes
                                    // keep writing them to the output stream
                                    while ((bytesRead = fs.Read(buffer, 0, AppConstants.BUFF_SIZE)) > 0)
                                    {
                                        targetStream.Write(buffer, 0, bytesRead);
                                        bytesOnTheStream += bytesRead;
                                    }

                                    fs.Close();
                                    targetStream.Close();

                                }
                                catch (Exception ex)
                                {
                                    throw;
                                } 

这里的问题是它没有显示在文本框中上传的文件的完整路径,只显示文件的名称。在代码后面,文本框没有唯一的名称。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

使用DirectoryInfo ClassFullName属性。

替换以下行:

string fullFileName = Path.Combine(targetFolder, fi.Name);

string fullFileName = Path.Combine(dir.FullName, fi.Name);