Unable to upload files in chunks on ownCloud server using C#

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Unable to upload files in chunks on ownCloud server using C#

rahuljain840
Hi All,

I am trying to upload file in chunks on ownCloud on-premise server using ASP.NET C#. I have tried all my ways to upload file in chunks but each time my chunk data is overwrite on previous chunk data.

I am unable to append my chunk on previously uploaded chunk. I am using plUpload to send files in chunk. And i have created an handler which is called each time a chunk is uploaded here is the code that runs to upload the chunk. I know i am missing some headers to inculde in code which must be required to submit data in chunks.
Below is my code to submit data on ownCloud server.

Source code used:

public void Upload(Stream chunkStream, String remoteFilePath, object state)
        {
            Uri uploadUri = getServerUrl(remoteFilePath, false);
            string method = WebRequestMethods.Http.Put.ToString();

            var content = ReadFully(chunkStream);

            AsyncCallback callback = new AsyncCallback(FinishUpload);

            // Need to send along headers?
            IDictionary<string, string> headers = new Dictionary<string, string>();
            headers.Add("OC_Chunked", "1");

            HTTPRequest(uploadUri, method, headers, content, localFilePath, callback, state, true);
        }

void HTTPRequest(Uri uri, string requestMethod, IDictionary<string, string> headers, byte[] content, string uploadFilePath, AsyncCallback callback, object state, bool sendChunks)
        {
            httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(uri);

            /*
             * The following line fixes an authentication problem explained here:
             * http://www.devnewsgroups.net/dotnetframework/t9525-http-protocol-violation-long.aspx
             */
            System.Net.ServicePointManager.Expect100Continue = false;

            // The server may use authentication
            if (user != null && pass != null)
            {
                NetworkCredential networkCredential;
                if (domain != null)
                {
                    networkCredential = new NetworkCredential(user, pass, domain);
                }
                else
                {
                    networkCredential = new NetworkCredential(user, pass);
                }
                httpWebRequest.Credentials = networkCredential;
                // Send authentication along with first request.
                httpWebRequest.PreAuthenticate = true;
            }
            httpWebRequest.Method = requestMethod;


            // httpWebRequest.SendChunked = true;
            if (headers != null)
            {
                foreach (string key in headers.Keys)
                {
                    httpWebRequest.Headers.Set(key, headers[key]);
                }
            }

            if (sendChunks)
            {
                httpWebRequest.SendChunked = true;
            }

            // Need to send along content?
            if (content != null || uploadFilePath != null)
            {
                RequestState asyncState = new RequestState();
                asyncState.request = httpWebRequest;
                asyncState.userCallback = callback;
                asyncState.userState = state;

                if (content != null)
                {
                    // The request either contains actual content...
                    httpWebRequest.ContentLength = content.Length;
                    asyncState.content = content;
                    httpWebRequest.ContentType = "text/xml";
                }
                else
                {
                    // ...or a reference to the file to be added as content.
                    httpWebRequest.ContentLength = new FileInfo(uploadFilePath).Length;
                    asyncState.uploadFilePath = uploadFilePath;
                }

                // Retrieve the request stream.
                Stream putRequestStream1 = httpWebRequest.GetRequestStream();

                // Write the string to the destination as text bytes.
                putRequestStream1.Write(content, 0, content.Length);

                // Close the request stream.
                putRequestStream1.Close();

                // Retrieve the response.
                HttpWebResponse httpPutResponse1 = (HttpWebResponse)httpWebRequest.GetResponse();
            }
            else
            {
            }
        }


Kindly suggest where am i doing wrong. 

Thanks in advance.

_______________________________________________
Devel mailing list
[hidden email]
http://mailman.owncloud.org/mailman/listinfo/devel
Reply | Threaded
Open this post in threaded view
|

Re: Unable to upload files in chunks on ownCloud server using C#

Vincent Petry
Hello,

Check out https://github.com/owncloud/pyocclient/blob/master/owncloud/owncloud.py#L457 for how to do it properly.

You need to pass a magic file name (file name + a bunch of stuff embedded in the file name) to make it work.

Cheers,

Vincent

On 07.06.2015 11:22, RAHUL JAIN wrote:
Hi All,

I am trying to upload file in chunks on ownCloud on-premise server using
ASP.NET C#. I have tried all my ways to upload file in chunks but each time
my chunk data is overwrite on previous chunk data.

I am unable to append my chunk on previously uploaded chunk. I am using
plUpload to send files in chunk. And i have created an handler which is
called each time a chunk is uploaded here is the code that runs to upload
the chunk. I know i am missing some headers to inculde in code which must
be required to submit data in chunks.
Below is my code to submit data on ownCloud server.

Source code used:

public void Upload(Stream chunkStream, String remoteFilePath, object state)
        {
            Uri uploadUri = getServerUrl(remoteFilePath, false);
            string method = WebRequestMethods.Http.Put.ToString();

            var content = ReadFully(chunkStream);

            AsyncCallback callback = new AsyncCallback(FinishUpload);

            // Need to send along headers?
            IDictionary<string, string> headers = new Dictionary<string,
string>();
            headers.Add("OC_Chunked", "1");

            HTTPRequest(uploadUri, method, headers, content, localFilePath,
callback, state, true);
        }

void HTTPRequest(Uri uri, string requestMethod, IDictionary<string, string>
headers, byte[] content, string uploadFilePath, AsyncCallback callback,
object state, bool sendChunks)
        {
            httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(uri);

            /*
             * The following line fixes an authentication problem explained
here:
             *
http://www.devnewsgroups.net/dotnetframework/t9525-http-protocol-violation-long.aspx
             */
            System.Net.ServicePointManager.Expect100Continue = false;

            // The server may use authentication
            if (user != null && pass != null)
            {
                NetworkCredential networkCredential;
                if (domain != null)
                {
                    networkCredential = new NetworkCredential(user, pass,
domain);
                }
                else
                {
                    networkCredential = new NetworkCredential(user, pass);
                }
                httpWebRequest.Credentials = networkCredential;
                // Send authentication along with first request.
                httpWebRequest.PreAuthenticate = true;
            }
            httpWebRequest.Method = requestMethod;


            // httpWebRequest.SendChunked = true;
            if (headers != null)
            {
                foreach (string key in headers.Keys)
                {
                    httpWebRequest.Headers.Set(key, headers[key]);
                }
            }

            if (sendChunks)
            {
                httpWebRequest.SendChunked = true;
            }

            // Need to send along content?
            if (content != null || uploadFilePath != null)
            {
                RequestState asyncState = new RequestState();
                asyncState.request = httpWebRequest;
                asyncState.userCallback = callback;
                asyncState.userState = state;

                if (content != null)
                {
                    // The request either contains actual content...
                    httpWebRequest.ContentLength = content.Length;
                    asyncState.content = content;
                    httpWebRequest.ContentType = "text/xml";
                }
                else
                {
                    // ...or a reference to the file to be added as content.
                    httpWebRequest.ContentLength = new
FileInfo(uploadFilePath).Length;
                    asyncState.uploadFilePath = uploadFilePath;
                }

                // Retrieve the request stream.
                Stream putRequestStream1 =
httpWebRequest.GetRequestStream();

                // Write the string to the destination as text bytes.
                putRequestStream1.Write(content, 0, content.Length);

                // Close the request stream.
                putRequestStream1.Close();

                // Retrieve the response.
                HttpWebResponse httpPutResponse1 =
(HttpWebResponse)httpWebRequest.GetResponse();
            }
            else
            {
            }
        }


Kindly suggest where am i doing wrong.

Thanks in advance.



_______________________________________________
Devel mailing list
[hidden email]
http://mailman.owncloud.org/mailman/listinfo/devel


_______________________________________________
Devel mailing list
[hidden email]
http://mailman.owncloud.org/mailman/listinfo/devel

signature.asc (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Unable to upload files in chunks on ownCloud server using C#

Vincent Petry
In reply to this post by rahuljain840
Hi,

I haven't found any official documentation about this and am also not
sure whether it qualifies as public API (public API as in "API that
needs to be kept stable").

Cheers,

Vincent


On 08.06.2015 10:23, Thomas Müller wrote:

>
>
>
> Vincent Petry – Mon., 8. Juni 2015 10:16
>> Hello,
>>
>>
>>     Check out
>>    
>> https://github.com/owncloud/pyocclient/blob/master/owncloud/owncloud.py#L457
>>     for how to do it properly.
>>
>
> Is this a documented feature as-in is this part of the public API?
>
> Generally speaking I'd prefer to keep such hacks private so that we
> can easily change this one day.
>
> Cheers,
>
> Tom
>
>>
>>     You need to pass a magic file name (file name + a bunch of stuff
>>     embedded in the file name) to make it work.
>>
>>
>>     Cheers,
>>
>>
>>     Vincent
>>
>

_______________________________________________
Devel mailing list
[hidden email]
http://mailman.owncloud.org/mailman/listinfo/devel
Reply | Threaded
Open this post in threaded view
|

Re: Unable to upload files in chunks on ownCloud server using C#

Vincent Petry
In reply to this post by rahuljain840
Hi,

I don't even think this can be changed in a short notice, because then
old sync client would not work any more.
So changing that API would need careful planning and deprecating older
sync clients.

But I agree, next time I'll mention "not public API, use at your own
risk" ;-)

Cheers,

Vincent

On 08.06.2015 10:40, Thomas Müller wrote:

>
>
>
> Vincent Petry – Mon., 8. Juni 2015 10:33
>> Hi,
>>
>> I haven't found any official documentation about this and am also not
>> sure whether it qualifies as public API (public API as in "API that
>> needs to be kept stable").
>>
>
> I'm not sure either ;-)
>
> I'd prefer to keep this private and advise people out there to use it
> on their own risk as
> we might want to change this on short notice.
>
> Thanks and take care,
>
> Thomas
>
>> Cheers,
>>
>> Vincent
>>
>>
>> On 08.06.2015 10:23, Thomas Müller wrote:
>> >
>> >
>> >
>> > Vincent Petry – Mon., 8. Juni 2015 10:16
>> >> Hello,
>> >>
>> >>
>> >>     Check out
>> >>    >>
>> https://github.com/owncloud/pyocclient/blob/master/owncloud/owncloud.py#L457
>> >>     for how to do it properly.
>> >>
>> >
>> > Is this a documented feature as-in is this part of the public API?
>> >
>> > Generally speaking I'd prefer to keep such hacks private so that we
>> > can easily change this one day.
>> >
>> > Cheers,
>> >
>> > Tom
>> >
>> >>
>> >>     You need to pass a magic file name (file name + a bunch of stuff
>> >>     embedded in the file name) to make it work.
>> >>
>> >>
>> >>     Cheers,
>> >>
>> >>
>> >>     Vincent
>> >>
>> >
>>
>> _______________________________________________
>> Devel mailing list
>> [hidden email]
>> http://mailman.owncloud.org/mailman/listinfo/devel
>>


_______________________________________________
Devel mailing list
[hidden email]
http://mailman.owncloud.org/mailman/listinfo/devel

signature.asc (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Unable to upload files in chunks on ownCloud server using C#

rahuljain840
In reply to this post by Vincent Petry
Hi All,

A lots of Thanks for your valuable suggestions and comments. I have checked the link provided by you guys and implemented in that way too. Still no success. But today i faced one more issue "Not Found - The requested URL /index.php/apps/files/ajax/download.php was not found on this server" while downloading the files from ownCloud server. 

Note: I am able to download files that are shared with me.

Steps to reproduce

1. Login into user account.
2. Create a folder e.g. /Test
3. Access external storage, create a text file ‘Test.txt' and save it.
4. Click download button.
5. Error msg 'Not Found' appears – “The requested URL /index.php/apps/files/ajax/download.php was not found on this server”.

Can anyone please suggest me is this can cause any trouble in chunk upload or how can i resolve this issue?

I found some links related to this issue: 
2. <a href="https://forum.owncloud.org/viewtopic.php?f=23&amp;t=11685#p30407https://forum.owncloud.org/viewtopic.php?f=23&amp;t=11685#p30407">https://forum.owncloud.org/viewtopic.php?f=23&t=11685#p30407https://forum.owncloud.org/viewtopic.php?f=23&t=11685#p30407

I really need to resolve this issue ASAP.


On Mon, Jun 8, 2015 at 1:46 PM, Vincent Petry <[hidden email]> wrote:
Hello,

Check out https://github.com/owncloud/pyocclient/blob/master/owncloud/owncloud.py#L457 for how to do it properly.

You need to pass a magic file name (file name + a bunch of stuff embedded in the file name) to make it work.

Cheers,

Vincent


On 07.06.2015 11:22, RAHUL JAIN wrote:
Hi All,

I am trying to upload file in chunks on ownCloud on-premise server using
ASP.NET C#. I have tried all my ways to upload file in chunks but each time
my chunk data is overwrite on previous chunk data.

I am unable to append my chunk on previously uploaded chunk. I am using
plUpload to send files in chunk. And i have created an handler which is
called each time a chunk is uploaded here is the code that runs to upload
the chunk. I know i am missing some headers to inculde in code which must
be required to submit data in chunks.
Below is my code to submit data on ownCloud server.

Source code used:

public void Upload(Stream chunkStream, String remoteFilePath, object state)
        {
            Uri uploadUri = getServerUrl(remoteFilePath, false);
            string method = WebRequestMethods.Http.Put.ToString();

            var content = ReadFully(chunkStream);

            AsyncCallback callback = new AsyncCallback(FinishUpload);

            // Need to send along headers?
            IDictionary<string, string> headers = new Dictionary<string,
string>();
            headers.Add("OC_Chunked", "1");

            HTTPRequest(uploadUri, method, headers, content, localFilePath,
callback, state, true);
        }

void HTTPRequest(Uri uri, string requestMethod, IDictionary<string, string>
headers, byte[] content, string uploadFilePath, AsyncCallback callback,
object state, bool sendChunks)
        {
            httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(uri);

            /*
             * The following line fixes an authentication problem explained
here:
             *
http://www.devnewsgroups.net/dotnetframework/t9525-http-protocol-violation-long.aspx
             */
            System.Net.ServicePointManager.Expect100Continue = false;

            // The server may use authentication
            if (user != null && pass != null)
            {
                NetworkCredential networkCredential;
                if (domain != null)
                {
                    networkCredential = new NetworkCredential(user, pass,
domain);
                }
                else
                {
                    networkCredential = new NetworkCredential(user, pass);
                }
                httpWebRequest.Credentials = networkCredential;
                // Send authentication along with first request.
                httpWebRequest.PreAuthenticate = true;
            }
            httpWebRequest.Method = requestMethod;


            // httpWebRequest.SendChunked = true;
            if (headers != null)
            {
                foreach (string key in headers.Keys)
                {
                    httpWebRequest.Headers.Set(key, headers[key]);
                }
            }

            if (sendChunks)
            {
                httpWebRequest.SendChunked = true;
            }

            // Need to send along content?
            if (content != null || uploadFilePath != null)
            {
                RequestState asyncState = new RequestState();
                asyncState.request = httpWebRequest;
                asyncState.userCallback = callback;
                asyncState.userState = state;

                if (content != null)
                {
                    // The request either contains actual content...
                    httpWebRequest.ContentLength = content.Length;
                    asyncState.content = content;
                    httpWebRequest.ContentType = "text/xml";
                }
                else
                {
                    // ...or a reference to the file to be added as content.
                    httpWebRequest.ContentLength = new
FileInfo(uploadFilePath).Length;
                    asyncState.uploadFilePath = uploadFilePath;
                }

                // Retrieve the request stream.
                Stream putRequestStream1 =
httpWebRequest.GetRequestStream();

                // Write the string to the destination as text bytes.
                putRequestStream1.Write(content, 0, content.Length);

                // Close the request stream.
                putRequestStream1.Close();

                // Retrieve the response.
                HttpWebResponse httpPutResponse1 =
(HttpWebResponse)httpWebRequest.GetResponse();
            }
            else
            {
            }
        }


Kindly suggest where am i doing wrong.

Thanks in advance.



_______________________________________________
Devel mailing list
[hidden email]
http://mailman.owncloud.org/mailman/listinfo/devel


_______________________________________________
Devel mailing list
[hidden email]
http://mailman.owncloud.org/mailman/listinfo/devel



_______________________________________________
Devel mailing list
[hidden email]
http://mailman.owncloud.org/mailman/listinfo/devel
Reply | Threaded
Open this post in threaded view
|

Re: Unable to upload files in chunks on ownCloud server using C#

Chris-3
CONTENTS DELETED
The author has deleted this message.