Link to home
Start Free TrialLog in
Avatar of fmichail
fmichailFlag for Canada

asked on

how to upload a local file to a subdomain

I have a subdomain subdomain1 in a primary domain maindomain1.com
I created a folder myfolder under the subdomain root, and granted public read/write access on

When I write the following code In my windows application,
           
            Dim upload As New System.Net.WebClient()
            upload.UploadFile("http://www.subdomain1.maindomain1.com/myfolder" , mylocalfilefullpath)

I am getting error 404 (non existing folder or file. What Am I doing wrong. Thanks for the help
Avatar of HainKurt
HainKurt
Flag of Canada image

use

 upload.UploadFile("http://subdomain1.maindomain1.com/myfolder" , mylocalfilefullpath)

Open in new window

Avatar of fmichail

ASKER

HainKurt,

How is your statement different from what I used... only removing www?. I will try it when I go to my office tonight, however, do you think that allowing public access to this folder ONLY is not enough, and the whole subdomain needs the same public permissions?
and I am assuming there is something on other side that will accept the uploaded file

a page like this

Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

        Dim f As String
        Dim file
        For Each f In Request.Files.AllKeys
            file = Request.Files(f)
            file.SaveAs("c:\inetpub\test\UploadedFiles\" & file.FileName)
        Next f

    End Sub

Open in new window


see

WebClient.UploadFile Method (String, String)
https://msdn.microsoft.com/en-us/library/36s52zhs(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
HAinKurt, Thanks For the reply,
I am using the same code as the example, however, I am getting error 404... The question is what do I need more than granting read/write permission to that folder to the public (which I did).

The other example is if I need to do that from a web page... What I need is to do it from winforms. Thanks
do you really have this url working?

http://www.subdomain1.maindomain1.com/myfolder

it should not return 404! first solve this part...
then we can look at upload part or permission or something else...
Hi Hainkurt

The real url is http://www.darelsalam.suredatasolutions.ca/client_photos/

This folder was assigned read/write permissions to public using fileZilla ftp tool. Please try to load any file to it using Uploadfile.

When trying to upload a file I get 403 error

403 - Forbidden: Access is denied.
You do not have permission to view this directory or page using the credentials that you supplied.

What Am I missing?

Thanks for your help
but this is http nothing to do ftp or filezilla!
you should check / set permission on IIS or Apache web server
the other thing is, probably you cannot upload a file like that!
you need a page that accepts uploaded files and store somewhere...

but first, lets fix the permission issue...
Hi Hainkurt

I managed to work around it using ftp protocol That I found in a site
Here is my code

My.Computer.Network.UploadFile("C:\myfile.jpg", "ftp://darelsalam.suredatasolutions.ca/client_photos/1.jpg", "ftp account user", "ftp account PW", True, 500)

I will have to encrypt the credentials in the configuration file, and use ftp for uploading

I would prefer to use the original webclient approach if you can figure it out, but the ftp is something like "work around" to me.
Thanks
for http you should have a page on the other site
that will accept uploaded files and save them somewhere else...
and you upload files to that page :)
and that page should not need to be in that folder...
and from web server, you should give permission write to scripts, so you can save them to that folder...
if ftp works fine, then why do you need http ???
you need a page + config on iis + lots of codes to get the files and rename  maybe and save them etc etc...
The only drawback I see in using the ftp is that I have to provide the credential in the code, Do you think that it makes a difference to allow write permission by requesting this from the host VS assigning the permission by going to the folder (using filezilla for example). If that is the case, I will contact the host and ask them to provide write permission on this folder and try again using the webclient object. However, when you say I need a page to upload, I do not understand exactly what should be in the page... remember, I need to Upload the files from Windows application not from website, uploading from website is easy using the upload control... this is not what I want. Thanks
I dont think you can upload files to web server using http without any accepting page on the server...

in http, you need to login, then in upload page, you check session info, if user is logged in, then accept the file and save it to server...
or pass user/pw to upload.asp page in same request, which will be the same thing as ftp

otherwise anyone can upload lots of illegal movies/files/junk here and your domain is flagged and you are kicked off by your hosting company :)

so, ftp solution seems much easier/cleaner...
Then what is the use of System.Net.WebClient() object UPLOAD method.

Remember, I have set the read/write permissions for public. This step cannot be done without knowing my credentials, so your concerns about misuse are not valid. Thanks
System.Net.WebClient() object UPLOAD method uploads a file to the url, a page...
then that page accepts the file and do something...
it is not like ftp
o/w anyone can upload anything to the folders!
Hi HainKurt,
I could not unerstand what you mean by "loading a file to a page" from windows application? is it passed as a querystring to the page or what?

The upload expects a resource (URI - not URL) to a file in the remote server, and expects a string containing the path to the local file to be uploaded, and it should upload this local file to the indicated resource in the remote server.

I may be unable to explain my point, but would appreciate so much if you can send me an example of loading a file (local file) to the folder client_photos on the subdomain darelsalam.suredatasolutions.ca . All permissions on that folder are already set for the public by the host

Thanks Hain
check this one...

WebClient.UploadFile Method (String, String, String)
https://msdn.microsoft.com/en-us/library/esst63h0(v=vs.110).aspx

it uploads a file using the method

public byte[] UploadFile(
	string address,
	string method,
	string fileName
)

Open in new window


so, on the other side, there should be something, a page, that accepts this file...
then do something with uploaded file...
it is not ftp, where you upload a file to a folder after connecting to ftp using username/password...
to upload to http, somehow you need credentials

upload.Credentials = new System.Net.NetworkCredential("username", "password", "domain")

Open in new window


and use PUT method

upload.UploadFile("http://www.subdomain1.maindomain1.com/myfolder" , "PUT", mylocalfilefullpath)

Open in new window


before using upload, set those as well...

if it does not work, you should add a upload.aspx page on the web server

there are samples here, vb and C#

WebClient.UploadFile Method (String, String)
https://msdn.microsoft.com/en-us/library/36s52zhs.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2

create that page and put into your web server and upload file to this page
and in this page, get the uploaded file and save wherever you want...

an customize this page as:

* you may check size/extension
* you may need to rename the file (file may already exists)
* you may save it to different folders if you wish
Dear HainKurt,

I will try using the URI format that you provided "http://subdomain1.maindomain1.com/myfolder", use "PUT" method as you indicated, however, I have the following concerns:

  1. If I need to provide the credential, then what is the point to grant the public read/write access to the folder
[list=2]Concerning the examples you provided, I am only interested on the first example (To upload from windows application not web application). And this is the example I used in my original code,  which showed the errors as indicated in my question as posted to experts exchange, so this is a kind of square 1 again [/list]
[list=3]Uploading from a webpage is not an option, and if it is I will use the Upload control which will handle the task very easily[/list]
[list=4]If I have to provide the credentials, Then I already have a solution using ftp call which I do not like because it uses the credentials[/list]
[list=5]I still cannot get "Load files to web page" should I say "http://mywebpage.aspx?filepath=c:\myfile1.jpg& filepath2=c:\myfile1.jpg....etc?
 Of course not !!, so how to call the webpage and attach to the request group of files? because if there is a way for that, then I will use it by  constructing the http request in the vb executable and use it. But before doing that, I need to know how to attach the files to the http request. Your help here is the key to the solution[/list]

Thanks again HainKurt
first of all, it does not matter how you upload the file, you need a mechanism on web server that accept the uploaded file, and it is the page which you can find on ms page

second, if it works fine with ftp, just use it...

you gave public read/write access to folder, I guess you did on your web server, to local users, not web users...
how did you set the permission from IIS?

I dont think, you can just upload files from your app to http and expect files will be written to the folder!
if thats the case, anybody can upload any files to your web server!

so,

upload.UploadFile("http://www.subdomain1.maindomain1.com/myfolder/myUpload.aspx" , "PUT", mylocalfilefullpath)

Open in new window


should post the file as "multipart/form-data"
try upload multiple files like

upload.UploadFile("http://www.subdomain1.maindomain1.com/myfolder/myUpload.aspx" , "PUT", mylocalfilefullpath1)
upload.UploadFile("http://www.subdomain1.maindomain1.com/myfolder/myUpload.aspx" , "PUT", mylocalfilefullpath2)
upload.UploadFile("http://www.subdomain1.maindomain1.com/myfolder/myUpload.aspx" , "PUT", mylocalfilefullpath3)

Open in new window


then on the accepting page, process each file like this:

Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

        Dim f As String
        Dim file
        For Each f In Request.Files.AllKeys
            file = Request.Files(f)
            file.SaveAs("c:\inetpub\test\UploadedFiles\" & file.FileName)
        Next f

End Sub

Open in new window

Hi HainKurt,
I think I started to understand what you mean. Here is my understanding of your ideas, please confirm if it is correct

Understanding (1)
===============
1- upload.UploadFile("http://www.subdomain1.maindomain1.com/myfolder/myUpload.aspx" , "PUT", mylocalfilefullpath1) will add a new resource to the page myupload.aspx the web page will not be automatically loaded
2- Repeat the step(1) for every file
3- Load the page from the windows Application which issued the upload commands. The page will process the request.files collection

Understanding (2)
===============
1- upload.UploadFile("http://www.subdomain1.maindomain1.com/myfolder/myUpload.aspx" , "PUT", mylocalfilefullpath1) will add a new resource to the page myupload.aspx the web page will not be automatically loaded
2- The page will load automatically and process the uploaded file
2- Repeat the step(1) and (2) for every file

Understanding (3)
===============
1- upload all the files in the folder (using *) using upload.UploadFile("http://www.subdomain1.maindomain1.com/myfolder/myUpload.aspx" , "PUT", mylocalfilefullpath1_*.*) will add a new resource to the page myupload.aspx
2- The page will load automatically and process the uploaded files

If any of these "Understandings" is correct , you have made the best solution.That sound logical to me, I really appreciate your idea

If the idea works the main attractive parts are:
(a) permissions are handled only within the web server
(b) No need to provide credential... It is all done withing the web server

To have a wonderful complete answer, I have 2 questions:

(1) Will the page load after each upload or I have to upload all the files then load the page?
(2) What do you mean by should post the file as "multipart/form-data". Please provide code
(3) if we upload all the files first (To compose request.files collection) how are these files attached to my process? in other words, if after uploading the files, some other person loads the page, of course these files that I uploaded will not be processed by him... The question is how the server knows that the page is opened by the uploading session not another session?

Great help HainKurt. I appreciate it
Fayez
your app will not load anything...

upload.UploadFile("http://www.subdomain1.maindomain1.com/myfolder/myUpload.aspx" , "PUT", mylocalfilefullpath1)

Open in new window


this will create a web request to the page, by providing an uploaded file
then myUpload.aspx will grab the submitted file and do whatever is needed with it (rename/move/copy/delete...)

I am not sure, if it can handle multiple files + extra form fields for username/password in same request though... need to test...

#3 will not work :) "mylocalfilefullpath1_*.*"

but, somehow, you need a login/pw so only you or your app can upload this way...
easiest way I guess is, when you do a request to that page, you also submit username/pw (maybe clientid or appid) + file
so, on this page, you check username/pw, then if they are ok, save the file... o/w dont do anything...

but all these are not better than ftp solution, which is very easy... just upload files to a folder with username/pw passed in same request...

basically you are trying to mimic ftp using http... only difference is like port number...
HainKurt,
I appreciate your help, I really learned a lot from you... Great help. One last question,

after calling the UploadFile method will the page be loaded automatically (and accordingly the page_load event is raised)?. In other words

suppose the webpage myupload.aspx is created with the page_load event handler as you indicated, if I use your code
upload.UploadFile("http://www.subdomain1.maindomain1.com/myfolder/myUpload.aspx" , "PUT", mylocalfilefullpath1)

Will that call create a webrequest to the page, and the page_load event is fired? OR I need to explicitly request to open the webpage after this statement?

Thanks again
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It was my pleasure to discuss and learn from you. Excellent job. Thanks a lot
I can't believe it, I was under the impression that I closed that question after accepting HainKurt very useful answer, however, when I logged in today, I found that the question is still open even after my comment on Jul 27...

Please HainKurt accept my apologies for this mistake. I will mark your answer as the best solution.... Sorry again
I can't believe it, I was under the impression that I closed that question after accepting HainKurt very useful answer, however, when I logged in today, I found that the question is still open even after my comment on Jul 27...

Please HainKurt accept my apologies for this mistake. I will mark your answer as the best solution.... Sorry again
no problem :)