Use IIS for file service and C # For upload and Deletion

Source: Internet
Author: User

Recently, the partner used IIS as the file server.
The partner has configured the following information for us:
An http ulr, user name, and password. It tells us that you can upload files through HTTP.
We cannot operate the partner server because we only know that it is an IIS server;
I found a lot of data and spent a lot of time learning how to upload files through IIS.

After searching for a long time, I did not find a lot of relevant information. Finally, I saw an English article on Microsoft's website.ArticleAnd test it slowly. Finally, solve the problem.
The put/delete operation supported by http1.1 is mainly used. Usually, we only get or post ..

For more information, see:
IIS configuration server:
1. Create a new site in IIS (detailed operation skipped)
2. Right-click the site and choose Properties> Home Directory => hook "write"
3. In "iisweb service extension", "WebDAV" is allowed (I did not choose this option, but it won't work after testing for half a day)
4. In the site directory, configure everyone to be fully operational (for security purposes, you can only allow full control of a specific user; you can use this user to upload/delete it later)

Upload files in C:
CodeAs follows:

Public   Void Uploadfilebinary ( String Localfile, String Uploadurl)
{
Httpwebrequest req = (Httpwebrequest) webrequest. Create (uploadurl );
Req. Credentials =   New Networkcredential ( " Administrator " , 123456 " ); // User name and password
Req. preauthenticate =   True ;
Req. Method =   " Put " ;
Req. allowwritestreambuffering =   True ;

// Retrieve request stream
Stream reqstream = Req. getrequeststream ();

// Open the local file
Filestream RDR =   New Filestream (localfile, filemode. Open );

// Allocate byte buffer to hold File Contents
Byte [] Indata =   New   Byte [ 4096 ];

// Loop through the local file reading each data block
// And writing to the request Stream Buffer
Int Bytesread = RDR. Read (indata, 0 , Indata. Length );
While (Bytesread >   0 )
{
Reqstream. Write (indata,0, Bytesread );
Bytesread=RDR. Read (indata,0, Indata. Length );
}

RDR. Close ();
Reqstream. Close ();

Req. getresponse ();
}

Code for deleting an object:

  Public   Void Deletefile ( String Uploadurl)
{
Httpwebrequest req = (Httpwebrequest) webrequest. Create (uploadurl );
Req. Credentials =   New Networkcredential ( " Administrator " , " 123456 " );
Req. preauthenticate =   True ;
Req. Method =   " Delete " ;
// Req. allowwritestreambuffering = true;

Req. getresponse ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.