c# - uploading a file using WCF in ASP.NET -
i have created 1 wcf service upload file. , after using service trying upload file able upload file there issue filestream class.
the moment clicked button upload file when checked debugging application know stream object null. passing object of stream class wcf method. but due issue stream object getting null. due null object of stream class, image uploded getting empty in folder
this code using upload file
if (fileupload1.hasfile) { system.io.fileinfo fileinfo = new system.io.fileinfo(fileupload1.postedfile.filename); filetransferservicereference.itransferservice clientupload = new filetransferservicereference.transferserviceclient("basichttpbinding_itransferservice"); filetransferservicereference.remotefileinfo uploadrequestinfo = new remotefileinfo(); string path = system.io.path.getdirectoryname(fileupload1.filename); using (system.io.filestream stream = new system.io.filestream(fileupload1.filename, system.io.filemode.open, system.io.fileaccess.read)) { uploadrequestinfo.filename = fileupload1.filename; uploadrequestinfo.length = fileinfo.length; uploadrequestinfo.filebytestream = stream; clientupload.uploadfile(uploadrequestinfo); } }
code wcf service
public remotefileinfo downloadfile(downloadrequest request) { remotefileinfo result = new remotefileinfo(); try { // info input file string filepath = system.io.path.combine(@"c:\uploadfiles", request.filename); system.io.fileinfo fileinfo = new system.io.fileinfo(filepath); // check if exists if (!fileinfo.exists) throw new system.io.filenotfoundexception("file not found", request.filename); // open stream system.io.filestream stream = new system.io.filestream(filepath, system.io.filemode.open, system.io.fileaccess.read); // return result result.filename = request.filename; result.length = fileinfo.length; result.filebytestream = stream; } catch (exception ex) { } return result; } public void uploadfile(remotefileinfo request) { filestream targetstream = null; stream sourcestream = request.filebytestream; string uploadfolder = @"c:\upload\"; if (!directory.exists(uploadfolder)) { directory.createdirectory(uploadfolder); } string filepath = path.combine(uploadfolder, request.filename); using (targetstream = new filestream(filepath, filemode.create, fileaccess.write, fileshare.none)) { const int bufferlen = 65000; byte[] buffer = new byte[bufferlen]; int count = 0; while ((count = sourcestream.read(buffer, 0, bufferlen)) > 0) { targetstream.write(buffer, 0, count); } targetstream.close(); sourcestream.close(); } } }
spot difference:
string uploadfolder = @"c:\upload\"; ... string filepath = system.io.path.combine(@"c:\uploadfiles", request.filename);
as general tip might put upload file path external configuration file, can change when move application onto server need store data on different drive or in specific location.
also way calling same configuration entry upload path name going same everywhere.
Comments
Post a Comment