ASP.Net: Writing chunks of file..HTTP File upload

2019-05-26 02:50发布

问题:

Please refer to question: Resume in upload file control

Now to find a solution for the above question, I want to work on it and develop a user control which can resume a HTTP File Upload process.

For this, I'm going to create a temporary file on server until the upload is complete. Once the uploading is done completely then will just save it to the desired location.

The procedure I have thought of is:

  1. Create a temporary file with a unique name (may be GUID) on the server.
  2. Read a chunk of file and append it to this temp file on the server.
  3. Continue step 1 to 3 until EOF is reached.
  4. Now if the connection is lost or user clicks on pause button, the writing of file is stopped and the temporary file is not deleted.
  5. Clicking on resume again or uploading the same file again will check on the server if the file exists and user whether to resume or to overwrite. (Not sure how to check if it's the same file. Also, how to step sending the chunks from client to server.)
  6. Clicking on resume will start from where it is required to be uploaded and will append that to the file on the server. (Again not sure how to do this.)

Questions:

  1. Are these steps correct to achieve the goal? Or need some modifications?
  2. I'm not sure how to implement all these steps. :-( Need ideas, links...

Any help appreciated...

回答1:

What you are trying is not easy, but it is doable. Follow these guidelines:

  1. Write 2 functions on the client side using ajax:

    • getUploadedPortionFromServer(filename) - This will ask the server if the file exists, and it should get an xml response from the server with the following info:
      boolean(exist/not exist), size(bytes), md5(string)
      The function will also run an md5 on the same size it got from the server on the local file,
      If the md5 is the same, you can continue sending from the point it was stopped,
      elseif not the same or size == 0, start over.
    • uploadFromBytes(x) - Will depend on the 1st function.

  2. On the server you have to write matching functions, which will check the needed stuff, and send the response via XML to the client.

    • In order to have distinct filenames, you should use some sort of user tagging.
    • Are users logged in to your server? if so, use a hash of thier username appended to the filename, this will help you distinguish between the files.
    • If not, use a cookie.