• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 562
  • Last Modified:

Upload file

Hi,

How can I modify the code to accept file from file upload control instead of hard coding as follows?

if (stlUtils.IsValidated(new string[] { @"c:\myfile.stl", ddlUnit.SelectedValue.ToString() }))

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

Please help.

ayha
0
ayha1999
Asked:
ayha1999
  • 6
  • 4
  • 3
1 Solution
 
käµfm³d 👽Commented:
if (stlUtils.IsValidated(new string[] { fuStlFile.FileName, ddlUnit.SelectedValue.ToString() }))

Open in new window

0
 
ayha1999Author Commented:
I tried both fuStlFile.FileName and fuStlFile.PostedFile.FileName but doesn't work.

the first one doesn't give any result, the other one gives an error

The process cannot access the file 'C:\myFile.stl' because it is being used by another process.
0
 
käµfm³d 👽Commented:
What is your goal? What are you attempting to do with the uploaded file?
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
ayha1999Author Commented:
calculating the volume of the file

        public double CalculateVolume2(string unit)
        {
            br = new BinaryReader(File.Open(_filename, FileMode.Open));
            byte[] buffer = new byte[128];

            br.Read(buffer, 0, 80);
...
0
 
käµfm³d 👽Commented:
The filename is more of a convenience for you, the developer, to know what the user had named the file (on their machine). That name doesn't exist on your machine--unless you write that file out to disk, and name it with that same name.

It appears that you are trying to get the bytes of the file. The good new for you is that the FileUpload control already provides you this via the FileBytes property. Simply modify you class a tad to have a new class-level member:

public class YourClass
{
    private byte[] _fileData;
...

Open in new window


...and then assign the file bytes to this new member wherever you are assigning the filename.

e.g.

...

public void SomeMethod()
{
    this._filename = fuStlFile.FileName;
    this._fileData = fuStlFile.FileBytes;
}

...

Open in new window


Then you can use this data within your other method:

public double CalculateVolume2(string unit)
{
    // use this._fileData here...it's already a byte array
    ...

Open in new window

0
 
Julian HansenCommented:
@Kaufmed

He is trying to merge the code from here http://www.experts-exchange.com/Programming/Languages/Scripting/Python/Q_28149820.html#a39229038
Into a file upload program.

System takes a .STL file and calculates the volume of the object described in the STL.

What he is looking for is how to take the uploaded file and pass it through to the STL class so it can process it based on the code in the above link.
0
 
ayha1999Author Commented:
this solution works


            fuStlFile.SaveAs(Server.MapPath("/files/"  + filename));

            if (stlUtils.IsValidated(new string[] {@Server.MapPath("/files/"+ filename), unit }))
            {
0
 
käµfm³d 👽Commented:
OK, but I still don't see why you'd write the bytes out to file just to read them back again--unless of course you need a copy of the file for some other reason. Just pass the byte array around.
0
 
Julian HansenCommented:
@kaufmed - did I miss the bit where he is writing out to a file?
0
 
käµfm³d 👽Commented:
Two posts up.
0
 
Julian HansenCommented:
I think that is just saving the uploaded file so it can be used by the STL class to calculate the volume of the object in the file ?
0
 
käµfm³d 👽Commented:
I think that is just saving the uploaded file...
Exactly my point. If you don't need a copy of the file for archival purposes, why write it out to disk at all? If you do need a copy, then the point is moot.
0
 
ayha1999Author Commented:
Thanks
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 6
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now