HEEN
HEEN

Reputation: 4721

Not getting the Uploaded filepath in asp.net

I want to save file to a specific location with some folder creation based on my requirement. So I wrote the below code.

public string CreateFilePath(string addedFolderName)
    {
        string folderPath = ConfigurationManager.AppSettings["DocDirectory"].ToString();

        string FileUplPath = folderPath + "\\" + addedFolderName + "\\";


        if (!Directory.Exists(FileUplPath))
        {   
            Directory.CreateDirectory(FileUplPath);
        }

        flUploadDocs.SaveAs(FileUplPath + Path.GetFileName(flUploadDocs.FileName));

        return folderPath;
    } 

But I am unable to get the filepath here. I am getting it as null

getting null at

Path.GetFileName(flUploadDocs.FileName)

<asp:FileUpload ID="flUploadDocs" runat="server" />

Please suggest what is wrong here.

Upvotes: 1

Views: 70

Answers (2)

Tyler S. Loeper
Tyler S. Loeper

Reputation: 836

The issue is that the webservice does not have the fileupload data. Here is the full code from our extended conversation:

    [WebMethod]
    public static string InsertUpdateMWSiteData(MWInsertUpdateFields MWInsertUpdateFields)
    {
        string strInsertUpdateMWInfo = "";
        try
        {
            Dashboard dshb = new Dashboard();

            dshb.CreateFilePath(MWInsertUpdateFields.SapID + "_" + MWInsertUpdateFields.CandidateID);

            strInsertUpdateMWInfo = CommonDB.InsertUpdateMWSiteInfo(MWInsertUpdateFields);
        }
        catch (Exception)
        {
            throw;
        }
        return strInsertUpdateMWInfo;
    }

    public string CreateFilePath(string addedFolderName)
    {
        string folderPath = ConfigurationManager.AppSettings["DocDirectory"].ToString();

        string FileUplPath = folderPath + "\\" + addedFolderName + "\\";


        if (!Directory.Exists(FileUplPath))
        {
            Directory.CreateDirectory(FileUplPath);
        }

        if (flUploadDoc.HasFile == true)
        {
            string strFilename = Path.GetFileName(flUploadDoc.FileName);
            flUploadDoc.SaveAs(FileUplPath + Path.GetFileName(flUploadDoc.PostedFile.FileName));
        }

        return folderPath;
    }

The problem is that after uploading a file, a request is sent to a webmethod which is being hosted in another instance of the program. This Webmethod checks its own instance for the fileupload control and data, and doesn't find it because it is in a different instance. This is why your fileupload control is returning null even on a sanity check of .HasFile().

One solution is to pass the data to the Webservice. You could for example pass the data to your webmethod as a byte[], and then on the webservice side reconvert it back into its original file type. After completing this process, save the file to your local filesystem. To do this you may need to pass the extension type and file name.

You may also want to add some validation to limit the file types accepted to only the most common file types like images, .doc, .excel, and whatever you have the library to support the conversion of.

If you want to save files directly to your filesystem using the upload control, you can do so but you will have to exclude the webservice step.

Please also see the discussion in chat for details.

Upvotes: 0

H.Mikhaeljan
H.Mikhaeljan

Reputation: 813

Path.GetFileName() returns the file name and extension of the specified path string

if im correct this only fills in the file name and not the directory + name.

Path.GetFileName(flUploadDocs.FileName)

possible solution

 Path.GetFileName(FileUplPath+flUploadDocs.FileName)

eventough im confused why you try to retrieve the path again after just having saved it?

Upvotes: 1

Related Questions