ASP.NET - i want to pass fixed path to file upload controler from code behind

Asked By sandeep Reddy on 22-Jan-12 06:45 AM
i want to pass fixed path to file upload controler from code behind

Thanks..
dipa ahuja replied to sandeep Reddy on 22-Jan-12 07:34 AM

<asp:FileUpload ID="FileUpload1" runat="server"></asp:FileUpload>
<asp:Button ID="UploadButton" Text="Upload file" OnClick="UploadButton_Click" runat="server"/>
 
 
protected void UploadButton1_Click(object sender, EventArgs e)
{
  if (FileUpload1.HasFile)
  {
    // Call a helper method routine to save the file.
    // Specify the path to save the uploaded file to.
    string savePath = Server.MapPath("images/");
 
    // Get the name of the file to upload.
    string fileName = FileUpload1.FileName;
 
    // Create the path and file name to check for duplicates.
    string pathToCheck = savePath + fileName;
 
    // Create a temporary file name to use for checking duplicates.
    string tempfileName = "";
 
    // Check to see if a file already exists with the, same name as the file to upload.      
    if (File.Exists(pathToCheck))
    {
      int counter = 2;
      while (File.Exists(pathToCheck))
      {
        // if a file with this name already exists,
        // prefix the filename with a number.
        tempfileName = counter.ToString() + fileName;
        pathToCheck = savePath + tempfileName;
        counter++;
      }
 
      fileName = tempfileName;
 
      // Notify the user that the file name was changed.
      UploadStatusLabel.Text = "A file with the same name already exists." +
        "<br />Your file was saved as " + fileName;
    }
    else
    {
      // Notify the user that the file was saved successfully.
      UploadStatusLabel.Text = "Your file was uploaded successfully.";
    }
 
    // Append the name of the file to upload to the path.
    savePath += fileName;
    FileUpload1.SaveAs(savePath);
     
    Image1.ImageUrl = "images/" + FileUpload1.FileName;
  }
  else
  {
    // Notify the user that a file was not uploaded.
    UploadStatusLabel.Text = "You did not specify a file to upload.";
  }
}
 
sandeep Reddy replied to dipa ahuja on 22-Jan-12 07:49 AM
Thanks for replay.
I need to set value to file uploader controler.i wont choose file from file uploader control.
i am getting file path from code behind. i need to assign value to file uploader or i can set value to context.request.files
dipa ahuja replied to sandeep Reddy on 22-Jan-12 08:12 AM
You cannot set the path because the property of of fileupload are readonly you can get the path but cannot set

And if you want to use the fix path then whats the need of fileupload control?? just take a variable and assign the path there


Danasegarane Arunachalam replied to sandeep Reddy on 22-Jan-12 09:45 AM
No. This cannot be done for security reasons. By default you cannot set any path in Fileupload control. This is restricted
sandeep Reddy replied to dipa ahuja on 22-Jan-12 11:51 AM
Thanks for u r replay,i knew that but my criteria is different.actually what i want is that i need to crop the image after that i need to save in to the sever.after save at the same time i need to send path to the HTTP handler as a context.response.files.
i don't have chance to edit HTTP handler so that i must pass through the file handler only.

thanks for u r help
dipa ahuja replied to sandeep Reddy on 22-Jan-12 11:57 AM
But once you get the posted file from the file upload , just save it, and then just pass that file to the handler, you have to change it any other way

You can save the file in global variable so you can use that in any where u req
Jitendra Faye replied to sandeep Reddy on 23-Jan-12 12:00 AM

You cannot.

For security reasons, the browser always requires the user to select the file that will be uploaded.