Hello,
I want to upload file on server but I get error then how to upload it, I use following code.
[WebMethod]
public string UploadFile(byte[] f, string fileName)
{
try
{
MemoryStream ms = new MemoryStream(f);
FileStream fs = new FileStream("http://103.16.141.197/ProBuildIndia/Operator/Sample/" + fileName, FileMode.Create);
ms.WriteTo(fs);
ms.Close();
fs.Close();
fs.Dispose();
return "OK";
}
catch (Exception ex)
{
return ex.Message.ToString();
}
}
Blackbarbie BbPosted Sep 2, 2015, 9:37 AM
Step 1: Add html input type as file
<input id =”uploadFile” type=”file” runat=”server” />
Step 2: Create a button to upload file in the server
<asp:Button ID=”Button1″ runat=”server” OnClick=”Button1_Click” />
Step 3: Create a folder in the web project like “TempFiles”
Step 4: Upload file in the server.
protectedvoidButton1_Click(objectsender, EventArgs e){if(this.uploadFile.PostedFile !=null){// Get a reference to PostedFile objectHttpPostedFile huploadFile = uploadFile.PostedFile;// Get size of uploaded fileintnFileLen = huploadFile.ContentLength;decimalfileSize = uploadFile.PostedFile.ContentLength / 1024;if(fileSize > 100)//If file size is no more than 100 KB{this.lblMsg.Text ="Size of this image is greater than 100KB";return;}byte[] scriptData =newbyte[nFileLen];// Read uploaded file from the StreamhuploadFile.InputStream.Read(scriptData, 0, nFileLen);stringfilePath = Server.MapPath("TempFiles");filePath = filePath +"\\"+ Path.GetFileName(uploadFile.PostedFile.FileName);//concate filename with file pathFileStream newFile =newFileStream(filePath, FileMode.Create);// Write data to the filenewFile.Write(scriptData, 0, scriptData.Length);// Close filenewFile.Close();}else{lblMsg.Text ="This is an empty file";}}